mysql 数据库查询测试_MySQL查询测试经验
測(cè)試表geoinfo,整個(gè)表超過(guò)1100萬(wàn)行,表結(jié)構(gòu):
CREATE TABLE`geoinfo` (
`objectid`int(11) NOT NULLAUTO_INCREMENT ,
`latitude`double NOT NULL,
`longitude`double NOT NULL,
`occupancy`bit(1) NOT NULL,
`time`datetime NOT NULL,
`cabid`varchar(16) NOT NULL,PRIMARY KEY(`objectid`),INDEX`idx_geoinfo_cabid_time`( `cabid`,`time`) USING BTREE
)
ENGINE=InnoDB
AUTO_INCREMENT=1
time字段為datetimes類(lèi)型,建立了與cabid字段(varchar類(lèi)型)的組合索引,整個(gè)表1100萬(wàn)+行。
測(cè)試結(jié)果:
1. between比"大于a and 小于b"效率稍高一點(diǎn)點(diǎn),僅僅是一點(diǎn)點(diǎn):
sql1:select time,objectid,cabid from geoinfo where time(time) BETWEEN time('07:00:00') and time('12:00:00')# and cabid='acitva'sql2:select time,objectid,cabid from geoinfo where time(time)>=time('07:00:00') and time(time)<=time('12:00:00')
sql1耗時(shí)10.180秒,sql2耗時(shí)11.760秒。
但一旦在where子句中加上cabid字段,即,select time,objectid,cabid from geoinfo where time(time) BETWEEN time('07:00:00') and time('12:00:00') and cabid='acitva'
耗時(shí)立刻減少到0.040秒。
2.or效率超過(guò)union all,且or的次數(shù)越多差距越明顯(與網(wǎng)上多數(shù)的所謂"優(yōu)化經(jīng)驗(yàn)"不同):
sql 3:select time,objectid,cabid from geoinfo where cabid like'a%' or cabid like 'b%' or cabid like 'e%'sql4:
select time,objectid,cabid from geoinfo where cabid like'a%'
union all select time,objectid,cabid from geoinfo where cabid like'b%'
union all select time,objectid,cabid from geoinfo where cabid like'e%'
sql3的執(zhí)行時(shí)間為6.590,7.090,6.880秒,多數(shù)為6.9秒以內(nèi);
sql4的執(zhí)行時(shí)間為7.892,8.452,7.912秒。兩者相差1-1.5秒。
sql 5:select time,objectid,cabid from geoinfo where cabid like'a%' or cabid like 'b%'sql6:select time,objectid,cabid from geoinfo where cabid like'a%'
union all select time,objectid,cabid from geoinfo where cabid like'b%'
sql 5的執(zhí)行時(shí)間依次為,3.050,3.089,3.200秒
sql6的執(zhí)行時(shí)間依次為,3.562,3.792,3.760秒,兩者相差0.5秒左右。
而把like改為'='號(hào)時(shí),
select time,objectid from geoinfo where cabid='udwadla' or cabid='osacmu' or cabid='unquekov'
or與union all差不多。
3.索引似乎只對(duì)簡(jiǎn)單的sql語(yǔ)句有提升,復(fù)雜一點(diǎn)的還是很慢。
例如:
select a.objectid as Aobjectid,b.objectid AS Bobjectid,a.time as Time fromgeoinfo_tiny a,geoinfo_tiny bwhere a.time=b.time and a.cabid='acitva' and b.cabid='abtyff'
其中對(duì)于geoinfo_tiny這樣只有280萬(wàn)行的表,該語(yǔ)句執(zhí)行時(shí)間就為95.361-100.004秒。索引似乎沒(méi)什么用了。
試試連接查詢:
select a.objectid as Aobjectid,b.objectid AS Bobjectid,a.time as Time fromgeoinfo_tiny ainner join geoinfo_tiny b on a.time=b.timewhere a.cabid='acitva' and b.cabid='abjoolaw'
多改幾次cabid的值,防止緩存,測(cè)試結(jié)果為95.635,39.172,85.862秒,可見(jiàn)連接查詢和多表查詢區(qū)別不大。
4.對(duì)于使用count這樣的聚合函數(shù),有索引照樣很慢。
總結(jié)
以上是生活随笔為你收集整理的mysql 数据库查询测试_MySQL查询测试经验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机怎么清理CAD,注册表cad,如何
- 下一篇: 12306的数据库设计