MySQL:统一 select 总是比单独的简单 select 快吗?

MySQL: Does unified select is always faster than seperated simple selects?

我有 3 个 table:

# config - only 1 line in table
# Config (maxCount int) 
Create Table Config(maxCount int)  
# files- 20k lines in table
# Files(fileid int (pk), filename varchar (unique), revCount int) 
Create Table Files ( 
   fileid int(10) unsigned not null auto_increment,
   filename varchar(255) not null,
   revCount int(10) unsigned default 1,
   primary key(fileid),
   unique key filename(filename) 
);
# Revitions - 26k lines in table
# Revisions(revid (pk),  fileid int (fk), revname varchar,...) 
create table Revisions( 
   revid int(10) unsigned not null auto_increment,
   fileid int(10) unsigned not null, 
   revname varchar(255) not null,
   mtime  timestamp default CURRENT_TIMESTAMP,
   deleted boolean default false,
   primary key(revid),
   KEY fr_fileid_fk_idx (fileid),
   CONSTRAINT fr_fileid_fk FOREIGN KEY(fileid)
     REFERENCE Files(fileid)
     ON DELETE CASCADE ON UPDATE CASCADE 
);

并具有以下存储过程:

create procedure test1(in file_id int,out max_count int, out rev_count int, out last_revid int)
begin 
  select maxCount into max_count from Config limit 1;
  select revCount into rev_count from Files where fileid=file_id;
  select max(revid) into last_revid from Revitions where fileid=file_id;
end

我通过为文件 (20k) 中的每个文件 ID 调用它来检查 test1 的性能,调用它花费了大约 2.7 秒到 运行

测试过程是:

create procedure t1()
begin
  declare done int default 0;
  declare file_id,last_revid,max_count,rev_cout int default 0;
  declare c1 cursor for select fileid from files;
  declare continue handle for not found select 1 into done from (select 1) as t;
  open c1;
  read1: loop
    fetch c1 into file_id;
    if done=1 then
      leave read1;
    end if;
    call test1(file_id, max_count,rev_cout,last_revid);
  end loop;
  close c1;
end 

我试过其他解决方案,将 3 个 select 统一为 1 个查询,如下所示:

create procedure test2(in file_id int,out max_count int, out rev_count int, out last_revid int)
begin 
  select maxCount, revCount, max(revid) 
  into   max_count,rev_count,last_revid 
  from Config, Files, Revitions 
  where Files.fileid=file_id AND Revitions.fileid=file_id
  limit 1;
end

我通过将 call test1 更改为 call test2

t1 更改为 t2

结果是明显更好的性能,第二个 (t2) 花了大约 2 秒(效率提高了 25%!),我重复了很多次测试,结果总是一样的。

有什么原因吗?

我认为 table 的连接效率低于每个 table 的 select 的效率,但显然第二个更快。

所以我可以指望它并且总是喜欢统一来自多个 table 的 select 查询,或者我应该检查每个案例哪个是最好的?

运行 单个组合查询并不总是比三个单独的查询更快。有时是,有时不是。

问:[单个组合查询比三个单独查询更快]有什么原因吗?

A:运行从一个过程中进行查询会产生开销,就像来自任何客户端一样。存在从过程到数据库引擎的上下文切换,以及语句的数据库引擎开销......解析,语法检查,语义检查,确定访问计划,执行查询,具体化结果并将其返回给客户端,作为以及语句关闭时的清理。

我们可以使用一个组合语句来避免一些这种开销,一次往返数据库,而不是三个单独的往返语句执行。

但不能保证组合语句会更快。这实际上取决于语句的执行计划,以及执行每个查询需要多少时间。

如果单个组合查询的执行时间明显高于三个单独语句的总执行时间,这将有效抵消任何开销节省,并且总体上可能会更慢。


问:我可以指望它并且总是喜欢统一来自多个表的 select 查询,还是我应该检查每个案例哪个是最好的?

A:经常组合查询会更快。但在一般情况下,我们不能 "count on" 它。我们需要检查每个案例。

只要你在比较技术,试试这个:

SELECT
  ( select maxCount FROM Config limit 1 ) AS max_count,
  ( select revCount FROM Files where fileid=file_id ) AS rev_count,
  ( select max(revid) FROM Revitions where fileid=file_id ) AS last_revid ;

(不需要存储过程。)