sql索引:sql索引2来源: 发布时间:星期三, 2008年12月17日 浏览:4次 评论:0
where fariqi<'2004-1-1' order by fariqi
用时:6453毫秒 ( 5)其他注意事项 “水可载舟 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 索引是从数据库中获取数据 ![]() ![]() ![]() ![]() ![]() 1. 不要索引常用 ![]() 不要为小型数据表设置任何键 ![]() ![]() ![]() ![]() ![]() 2. 不要把社会保障号码(SSN)或身份证号码(ID)选作键 永远都不要使用 SSN 或 ID 作为数据库 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 3. 不要用用户 ![]() 在确定采用什么字段作为表 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 4. 不要索引 memo/notes 字段和不要索引大型文本字段(许多 ![]() 这样做会让你 ![]() ![]() 5. 使用系统生成 ![]() 假如你总是在设计数据库 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 采用系统生成键作为主键还有 ![]() ![]() ![]() ![]() ![]() 2、改善SQL语句 很多人不知道SQL语句在SQL SERVER中是如何执行 ![]() ![]() ![]() ![]() select * from table1 where name='zhangsan' and tID > 10000 和执行: select * from table1 where tID > 10000 and name='zhangsan' ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 事实上 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 虽然查询优化器可以根据where子句自动 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 在查询分析阶段 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() SARG ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 列名 操作符 <常数 或 变量> 或 <常数 或 变量> 操作符列名 列名可以出现在操作符 ![]() ![]() ![]() ![]() ![]() ![]() Name=’张 3’ 价格>5000 5000<价格 Name=’张 3’ and 价格>5000 如果 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 介绍完SARG后 ![]() ![]() ![]() ![]() 1、Like语句是否属于SARG取决于所使用 ![]() ![]() 如:name like ‘张%’ ![]() 而:name like ‘%张’ ,就不属于SARG ![]() 原因是通配符%在 ![]() ![]() ![]() 2、or 会引起全表扫描 如:Name=’张 3’ and 价格>5000 符号SARG ![]() 而:Name=’张 3’ or 价格>5000 则不符合SARG ![]() 使用or会引起全表扫描 ![]() 3、非操作符、 ![]() ![]() ![]() 不满足SARG形式 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ABS(价格)<5000 Name like ‘% 3’ 有些表达式 ![]() WHERE 价格*2>5000 SQL SERVER也会认为是SARG ![]() WHERE 价格>2500/2 但我们不推荐这样使用 ![]() ![]() ![]() ![]() 4、IN ![]() 语句: Select * from table1 where tid in (2,3) 和 Select * from table1 where tid=2 or tid=3 是 ![]() ![]() ![]() ![]() ![]() ![]() 5、尽量少用NOT 6、exists 和 in ![]() ![]() ![]() 很多资料上都显示说 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() (1)select title,price from titles where title_id in (select title_id from sales where qty>30) 该句 ![]() 表 'sales' ![]() ![]() ![]() ![]() ![]() 表 'titles' ![]() ![]() ![]() ![]() ![]() (2)select title,price from titles where exists (select * from sales where sales.title_id=titles.title_id and qty>30) 第 2句 ![]() 表 'sales' ![]() ![]() ![]() ![]() ![]() 表 'titles' ![]() ![]() ![]() ![]() ![]() 我们从此可以看到用exists和用in ![]() ![]() ![]() ![]() 7、用 ![]() ![]() ![]() ![]() 前面 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() select gid,title,fariqi,reader from tgongwen where charindex('刑侦支队',reader)>0 and fariqi>'2004-5-5' 用时:7秒 ![]() ![]() ![]() ![]() ![]() select gid,title,fariqi,reader from tgongwen where reader like '%' + '刑侦支队' + '%' and fariqi>'2004-5-5' 用时:7秒 ![]() ![]() ![]() ![]() ![]() 8、union并不绝对比or ![]() 我们前面已经谈到了在where子句中使用or会引起全表扫描 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' or gid>9990000 用时:68秒 ![]() ![]() ![]() ![]() ![]() select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' union select gid,fariqi,neibuyonghu,reader,title from Tgongwen where gid>9990000 用时:9秒 ![]() ![]() ![]() ![]() ![]() 看来 ![]() ![]() ![]() ![]() 但经过试验 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' or fariqi='2004-2-5' 用时:6423毫秒 ![]() ![]() ![]() ![]() ![]() select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-9-16' union select gid,fariqi,neibuyonghu,reader,title from Tgongwen where fariqi='2004-2-5' 用时:11640毫秒 ![]() ![]() ![]() ![]() ![]() 9、字段提取要按照“需多少、提多少” ![]() ![]() 我们来做 ![]() select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc 用时:4673毫秒 select top 10000 gid,fariqi,title from tgongwen order by gid desc 用时:1376毫秒 select top 10000 gid,fariqi from tgongwen order by gid desc 用时:80毫秒 由此看来 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 10、count(*)不比count(字段)慢 某些资料上说:用*会统计所有列 ![]() ![]() ![]() ![]() ![]() ![]() select count(*) from Tgongwen 用时:1500毫秒 select count(gid) from Tgongwen 用时:1483毫秒 select count(fariqi) from Tgongwen 用时:3140毫秒 select count(title) from Tgongwen 用时:52050毫秒 从以上可以看出 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 11、order by按聚集索引列排序效率最高 我们来看:(gid是主键 ![]() select top 10000 gid,fariqi,reader,title from tgongwen 用时:196 毫秒 ![]() ![]() ![]() ![]() ![]() select top 10000 gid,fariqi,reader,title from tgongwen order by gid asc 用时:4720毫秒 ![]() ![]() ![]() ![]() ![]() select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc 用时:4736毫秒 ![]() ![]() ![]() ![]() ![]() select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi asc 用时:173毫秒 ![]() ![]() ![]() ![]() ![]() select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi desc 用时:156毫秒 ![]() ![]() ![]() ![]() ![]() 从以上我们可以看出 ![]() ![]() 0
相关文章
读者评论
发表评论 |