MySQL查询测试经验
测试表geoinfo,整个表超过1100万行,表结构:
CREATE TABLE `geoinfo` (
`objectid` int(11) NOT NULL AUTO_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类型,建立了与cabid字段(varchar类型)的组合索引,整个表1100万+行。
测试结果:
1. between比"大于a and 小于b"效率稍高一点点,仅仅是一点点:
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耗时10.180秒,sql2耗时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'
耗时立刻减少到0.040秒。
2.or效率超过union all,且or的次数越多差距越明显(与网上多数的所谓"优化经验"不同):
sql 3:select time,objectid,cabid from geoinfo where cabid like'a%' or cabid like 'b%' or cabid like 'e%' sql 4:
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的执行时间为6.590,7.090,6.880秒,多数为6.9秒以内;
sql4的执行时间为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%'
sql 6:
select time,objectid,cabid from geoinfo where cabid like'a%'
union all select time,objectid,cabid from geoinfo where cabid like'b%'
sql 5的执行时间依次为,3.050,3.089,3.200秒
sql6的执行时间依次为,3.562,3.792,3.760秒,两者相差0.5秒左右。
而把like改为'='号时,
select time,objectid from geoinfo where cabid='udwadla' or cabid='osacmu' or cabid='unquekov'
or与union all差不多。
3.索引似乎只对简单的sql语句有提升,复杂一点的还是很慢。
例如:
select a.objectid as Aobjectid,b.objectid AS Bobjectid,a.time as Time from geoinfo_tiny a,geoinfo_tiny b
where a.time=b.time and a.cabid='acitva' and b.cabid='abtyff'
其中对于geoinfo_tiny这样只有280万行的表,该语句执行时间就为95.361-100.004秒。索引似乎没什么用了。
试试连接查询:
select a.objectid as Aobjectid,b.objectid AS Bobjectid,a.time as Time from geoinfo_tiny a
inner join geoinfo_tiny b on a.time=b.time
where a.cabid='acitva' and b.cabid='abjoolaw'
多改几次cabid的值,防止缓存,测试结果为95.635,39.172,85.862秒,可见连接查询和多表查询区别不大。
4.对于使用count这样的聚合函数,有索引照样很慢。
MySQL查询测试经验的更多相关文章
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
		MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ... 
- 生产要不要开启MySQL查询缓存
		一.前言 在当今的各种系统中,缓存是对系统性能优化的重要手段.MySQL Query Cache(MySQL查询缓存)在MySQL Server中是默认打开的,但是网上各种资料以及有经验的DBA都建议 ... 
- 一个诡异的MySQL查询超时问题,居然隐藏着存在了两年的BUG
		这一周线上碰到一个诡异的BUG. 线上有个定时任务,这个任务需要查询一个表几天范围内的一些数据做一些处理,每隔十分钟执行一次,直至成功. 通过日志发现,从凌晨5:26分开始到5:56任务执行了三次,三 ... 
- Mysql 查询练习
		Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ... 
- mysql查询中通配符的使用
		mysql查询中通配符的使用 在mysql查询中经常会使用通配符,并且mysql的通配符和pgsql的存在区别(稍候再讨论),而且mysql中还可以使用正则表达式. SQL模式匹配: “_” ... 
- memcached +mysql+php 测试例子
		最近看了看memcached 缓存 mysql,经过一段时间的研究·把自己测试方法写出来,给像我一样的初学者用~我的环境是 nginx + php-fpm + apc + mysql 编译安装memc ... 
- MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图
		http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ... 
- mysql 查询缓存配置和查看
		Mysql 查询缓存 查询缓存的作用就是当查询接收到一个和之前同样的查询,服务器将会从查询缓存种检索结果,而不是再次分析和执行上次的查询.这样就大大提高了性能,节省时间. 1.配置查询缓存 修改配置文 ... 
- MySQL索引实战经验总结
		MySQL索引对数据检索的性能至关重要,盲目的增加索引不仅不能带来性能的提升,反而会消耗更多的额外资源,本篇总结了一些MySQL索引实战经验. 索引是用于快速查找记录的一种数据结构.索引就像是数据库中 ... 
随机推荐
- 第二课:判断js变量的类型以及domReady的原理
			1.类型的判断: js五种简单数据类型有:null,undefined,boolean,number,string. 还有复杂的数据类型:Object,Function,RegExp,Date,自定义 ... 
- FilenameFilter用法
			使用FilenameFilter实现图片过滤,只要.gif,.jpg,.png文件. java 代码 public class ImageFilter implements FilenameFilte ... 
- Tomcat 在win7/win8 系统下tomcat-users.xml.new(拒绝访问)解决方法
			tomcat启动报错No UserDatabase component found under key UserDatabase 也可以这样处理 Tomcat 在win7/win8 系统下tomcat ... 
- JS模式:又一个简单的工厂模式
			<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ... 
- 读JS高级API笔记_(DOM&&DOM2&&DOM3)哎呀——园龄才9个月啊
			---恢复内容开始--- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ... 
- Java-人民币转成大写
			/** * 人民币转成大写 hangeToBig * * @param value * @return String */ public static String 人民币转成大写(double va ... 
- 加载数据库驱动程序的方法和JDBC的流程
			加载驱动方法 1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.r ... 
- 【bzoj1046】 HAOI2007—上升序列
			http://www.lydsy.com/JudgeOnline/problem.php?id=1046 (题目链接) 题意 给出一个数列,求数列中长度为L的下标字典序最小的上升子序列. Soluti ... 
- POJ1743 Musical Theme
			Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are int ... 
- Pseudo-elements ::before, ::after 範例
			xhtml <strong class="amount">700</strong> css .amount::before {content:"$ ... 
