当前位置:数据库 > SQLServer >>

MySQL”海量数据”查询性能分析

最近做了一次MySQL所谓的”海量数据”查询性能分析.

表结构

dt dt2 dt3 it it2 it3  
id id id id id id int PK
    ext1     ext1 varchar(256)
time time time time time time int/datetime KEY
  ext2 ext2   ext2 ext2 varchar(128)

说明, MyISAM引擎, dt表示时间字段使用datetime类型, it表示时间字段使用int类型.

初始数据

首先生成100K个UNIX时间戳(int), 然后随机选取10M次, 每一次往6个表里插入一条记录(当time字段是datetime类型时, 做类型转换). 所以每一个表都有10M条记录. ext1和ext2字段会用随机的字符串填充.

 

SQL查询

使用的查询SQL语句如:

select SQL_NO_CACHE count(*) from it where time>10000;
select SQL_NO_CACHE count(*) from dt where time>from_unixtime(10000);
select SQL_NO_CACHE * from it where time>10000 order by time limit 1;
select SQL_NO_CACHE * from it use key(PRIMARY) where time>10000 order by id limit 1;

SQL_NO_CACHE用于消除查询结果缓存的影响. use key用于指定查询时使用的索引. 统计每一条SQL的执行时间(单位s)和满足WHERE条件的记录总数(total), it-tm表示在dt表上执行SQL的耗时, 并explain得到key和extra, 结果如下.

where total select orderby key it-tm dt-tm it2-tm dt2-tm it3-tm dt3-tm extra
time>10000 8999050 count(*)   time 3.52 4.28 3.74 4.49 3.53 4.47 where; index
    count(time)   time 3.44 4.00 3.69 4.36 3.56 4.26 where; index
    count(id)   NULL 1.44 1.92 4.30 4.60 4.79 4.98 where
    * time time 0.00 0.00 0.00 0.00 0.00 0.00 where
    * id time 14.81 15.38 19.37 20.30 20.94 21.42 where; filesort
    * id PK 0.00 0.03 0.00 0.02 0.00 0.04 where
                       
time>50000 4987990 count(*)     1.90 2.36 2.02 2.41 1.99 2.42  
    count(time)     1.90 2.23 2.01 2.32 1.96 2.29  
    count(id)     1.48 1.91 4.25 4.61 4.80 5.12  
    * time   0.00 0.0
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,