- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章MySQL去重该使用distinct还是group by?由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
关于group by 与distinct 性能对比:网上结论如下,不走索引少量数据distinct性能更好,大数据量group by 性能好,走索引group by性能好。走索引时分组种类少distinct快。关于网上的结论做一次验证.
准备阶段屏蔽查询缓存 。
查看mysql中是否设置了查询缓存。为了不影响测试结果,需要关闭查询缓存.
1
|
show variables
like
'%query_cache%'
;
|
查看是否开启查询缓存决定于query_cache_type和query_cache_size.
query_cache_type
需要修改c:\programdata\mysql\mysql server 5.7\my.ini配置文件,修改query_cache_type=0或2
。query_cache_size
为0,执行以下语句。
1
|
set
global
query_cache_size = 0;
|
方法三:如果你不想关闭查询缓存,也可以在使用reset query cache.
现在测试环境中query_cache_type=2代表按需进行查询缓存,默认的查询方式是不会进行缓存,如需缓存则需要在查询语句中加上sql_cache.
数据准备 。
t0表存放10w少量种类少的数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
drop
table
if exists t0;
create
table
t0(
id
bigint
primary
key
auto_increment,
a
varchar
(255)
not
null
) engine=innodb
default
charset=utf8mb4
collate
=utf8mb4_bin;
1
2
3
4
5
drop
procedure
insert_t0_simple_category_data_sp;
delimiter //
create
procedure
insert_t0_simple_category_data_sp(
in
num
int
)
begin
set
@i = 0;
while @i < num do
insert
into
t0(a) value(
truncate
(@i/1000, 0));
set
@i = @i + 1;
end
while;
end
//
call insert_t0_simple_category_data_sp(100000);
|
t1表存放1w少量种类多的数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
drop
table
if exists t1;
create
table
t1
like
t0;
1
2
drop
procedure
insert_t1_complex_category_data_sp;
delimiter //
create
procedure
insert_t1_complex_category_data_sp(
in
num
int
)
begin
set
@i = 0;
while @i < num do
insert
into
t1(a) value(
truncate
(@i/10, 0));
set
@i = @i + 1;
end
while;
end
//
call insert_t1_complex_category_data_sp(10000);
|
t2表存放500w大量种类多的数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
drop
table
if exists t2;
create
table
t2
like
t1;
1
2
drop
procedure
insert_t2_complex_category_data_sp;
delimiter //
create
procedure
insert_t2_complex_category_data_sp(
in
num
int
)
begin
set
@i = 0;
while @i < num do
insert
into
t1(a) value(
truncate
(@i/10, 0));
set
@i = @i + 1;
end
while;
end
//
call insert_t2_complex_category_data_sp(5000000);
|
测试阶段 。
验证少量种类少数据 。
未加索引 。
1
2
3
4
5
6
|
set
profiling = 1;
select
distinct
a
from
t0;
show profiles;
select
a
from
t0
group
by
a;
show profiles;
alter
table
t0
add
index
`a_t0_index`(a);
|
由此可见:少量种类少数据下,未加索引,distinct和group by性能相差无几.
加索引 。
1
|
alter
table
t0
add
index
`a_t0_index`(a);
|
执行上述类似查询后 。
由此可见:少量种类少数据下,加索引,distinct和group by性能相差无几.
验证少量种类多数据未加索引 。
执行上述类似未加索引查询后 。
由此可见:少量种类多数据下,未加索引,distinct比group by性能略高,差距并不大.
加索引 。
1
|
alter
table
t1
add
index
`a_t1_index`(a);
|
执行类似未加索引查询后 。
由此可见:少量种类多数据下,加索引,distinct和group by性能相差无几.
验证大量种类多数据 。
未加索引 。
1
|
select
count
(1)
from
t2;
|
执行上述类似未加索引查询后 。
由此可见:大量种类多数据下,未加索引,distinct比group by性能高.
加索引 。
1
|
alter
table
t2
add
index
`a_t2_index`(a);
|
执行上述类似加索引查询后 。
由此可见:大量种类多数据下,加索引,distinct和group by性能相差无几.
总结 性能比 少量种类少 少量种类多 大量种类多未加索引相差无几distinct略优distinct更优加索引相差无几相差无几相差无几 。
去重场景下,未加索引时,更偏向于使用distinct,而加索引时,distinct和group by两者都可以使用.
总结 。
到此这篇关于mysql去重该使用distinct还是group by?的文章就介绍到这了,更多相关mysql 去重distinct group by内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/NestorBian/article/details/106004840 。
最后此篇关于MySQL去重该使用distinct还是group by?的文章就讲到这里了,如果你想了解更多关于MySQL去重该使用distinct还是group by?的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个包含电子邮件、IP、州、城市、时间戳、ID 列的表 我需要按州分组计算电子邮件和 IP 的不同位置 所以当我运行 MYSQL 查询时, select State, City ,count(di
我试过 select distinct ID from DB.TABLE; 它返回所有记录中的唯一 ID。 select distinct * from DB.TABLE; 它将通过比较所有列
我正在尝试在 Postgresql 中编写一个查询,该查询提取一组有序数据并按不同的字段对其进行过滤。我还需要从同一表行中提取其他几个字段,但需要将它们排除在不同的评估之外。示例: SELECT
我有一个使用以下语句创建的 Postgres 表。该表由另一个服务的数据转储填充。 CREATE TABLE data_table ( date date DEFAULT NULL,
我在一个名为 products 的表中有 4 列 id|p_name| p_img | 1 | Xs | xsmax.png | 2 | Xs | xr.png |
当它的状态仅为"is"时,我想从“num”中选择不同的值,而不是立即包括“否”? 表: +--------+-----+--------+ | id | num | status | +---
全部!今天我有一个棘手的问题要给你,我想使用 select DISTINCT 语句来选择一个需要不同的行,但也在同一个语句中(或者我尝试过的方式?)一个没有的行't/不能区分。我想要的结果是每个类名中
我有一个正在使用 Distinct() 的 linq 查询。如果我只是调用 Distinct() 而没有转换为列表,那么它不会返回不同的列表 - 它仍然包含重复项。 但是,如果我转换为 List 并然
说到性能,我应该使用 .ToList().Distinct() 还是 .Distinct().ToList() ? 两种扩展方法是否生成相同的 SQL 查询? 看起来第二种方法应该表现更好,但这是真的
如何在不支持 SQL Server 2008R2 的 SQL 实现中重写包含标准 IS DISTINCT FROM 和 IS NOT DISTINCT FROM 运算符的表达式? 最佳答案 IS DI
有一张 table (在 HIVE) 示例 - meanalytics.key2_master_ids 该表有 6 列(cmpgn_id、offr_id、exec_id、creatv_id、cmpl_
SELECT * FROM `amc_info` WHERE department =' ( SELECT DISTINCT department ) into outfile = 'Differe
如何在Elasticsearch中计算“不同的平均值”?我有一些这样的非规范化数据: { "record_id" : "100", "cost" : 42 } { "record_id" : "200
关注这个question我有... ID SKU PRODUCT ======================= 1 FOO-23 Orange 2 BAR
我有这个 mysql 查询: SELECT DISTINCT post.postId,hash,previewUrl,lastRetrieved FROM post INNER JOIN (tag a
http://sqlfiddle.com/#!2/37dd94/17 如果我执行 SELECT DISTINCT,我得到的结果与只执行 SELECT 的结果相同。 在查询结果中,您将看到两个包含 Di
我有一列包含空条目,例如此列中的可能值为 None, 1, 2, 3 当我使用 session.query(func.count(distinct(Entry.col))).scalar() 计算列中
这是否可能从表列中选择不同的行并计算单个查询中每个不同字段的重复行 $sql = "SELECT DISTINCT location and COUNT(DISTINCT location)
我在 MySQL 数据库中有一个包含 1100 万行的表。其中一列是个人身份证号码。人们在表中被多次列出,我想知道有多少个唯一的个人 ID 号码。然后创建一个包含这些唯一数字的表格。当我计算列中不同的
我刚刚注意到我的 Informix SQL 列(在同一个表中)的某些 上有些奇怪。当我执行此查询时 SELECT DISTINCT colName FROM myTable 例如,我得到 40 行。但
我是一名优秀的程序员,十分优秀!