正 文:
说起数据库的SQL语句执行效率的问题,就不得不提where条件语句中的or(逻辑或)引起的全表扫描问题,从而导致效率下降。
|
SET STATISTICS profile ON
SET STATISTICS io ON
SET STATISTICS time ON
go
select * from chuzu where c_id>1000 or c_qu='沙坪坝区' order by c_time desc
union
select * from chuzu where c_qu='沙坪坝区' order by c_time desc
go
SET STATISTICS profile OFF
SET STATISTICS io OFF
SET STATISTICS time OFF
SET STATISTICS io ON
SET STATISTICS time ON
go
select * from chuzu where c_id>1000 or c_qu='沙坪坝区' order by c_time desc
union
select * from chuzu where c_qu='沙坪坝区' order by c_time desc
go
SET STATISTICS profile OFF
SET STATISTICS io OFF
SET STATISTICS time OFF

(所影响的行数为 52713 行)
表 'chuzu'。扫描计数 1,逻辑读 2412 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 938 毫秒,耗费时间 = 3222 毫秒。
表 'chuzu'。扫描计数 2,逻辑读 4774 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 1484 毫秒,耗费时间 = 4323 毫秒。
表 'chuzu'。扫描计数 1,逻辑读 2412 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 938 毫秒,耗费时间 = 3222 毫秒。
表 'chuzu'。扫描计数 2,逻辑读 4774 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 1484 毫秒,耗费时间 = 4323 毫秒。
SET STATISTICS profile ON
SET STATISTICS io ON
SET STATISTICS time ON
go
select * from chuzu where c_qu='九龙坡区' or c_qu='沙坪坝区' order by c_time desc
union
select * from chuzu where c_qu='沙坪坝区' order by c_time desc
go
SET STATISTICS profile OFF
SET STATISTICS io OFF
SET STATISTICS time OFF
SET STATISTICS io ON
SET STATISTICS time ON
go
select * from chuzu where c_qu='九龙坡区' or c_qu='沙坪坝区' order by c_time desc
union
select * from chuzu where c_qu='沙坪坝区' order by c_time desc
go
SET STATISTICS profile OFF
SET STATISTICS io OFF
SET STATISTICS time OFF

(所影响的行数为 6131 行)
表 'chuzu'。扫描计数 1,逻辑读 2412 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 203 毫秒,耗费时间 = 635 毫秒。
表 'chuzu'。扫描计数 2,逻辑读 4824 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 360 毫秒,耗费时间 = 798 毫秒。
表 'chuzu'。扫描计数 1,逻辑读 2412 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 203 毫秒,耗费时间 = 635 毫秒。
表 'chuzu'。扫描计数 2,逻辑读 4824 次,物理读 0 次,预读 0 次。
SQL Server 执行时间: CPU 时间 = 360 毫秒,耗费时间 = 798 毫秒。