SQLSERVER2008R2 索引建立的几点建议
T1表10000000万条数据,(插入时间36分钟,count(*)查询19秒,空间占用670M左右) 1.真正充分的利用索引 比如like'张%'就是符合SARG(符合扫描参数)标准 而like'%张'就不符合该标准 通配符%在字符串首字符的使用会导致索引无法使用,虽然实际应用中很难避免这
T1表 10000000万条数据,(插入时间36分钟,count(*)查询19秒,空间占用670M左右)
1.真正充分的利用索引
比如like '张%' 就是符合SARG(符合扫描参数)标准
而like '%张' 就不符合该标准
通配符%在字符串首字符的使用会导致索引无法使用,虽然实际应用中很难避免这样用,但还是应该对这种现象有所了解,至少知道此种用法性能是很低下的。
**********************************************
2.“非”操作符不满足SARG形式,使得索引无法使用
不满足SARG形式的语句最典型的情况就是包括非操作符的语句,如:NOT、!=、、!、NOT EXISTS、NOT IN、NOT LIKE等。
如果使用not 或者 ,最好转换成别的方法,比如例子如下:
T1表 10000000万条数据,构建如下:(插入时间36分钟,count(*)查询19秒,空间占用670M左右)
DECLARE @i INT
SET @i = 1
WHILE @i
BEGIN
INSERT INTO t1 VALUES ('zhang'+CONVERT(char(50), @i),'3.2',77);
SET @i + 1;
END
三种查询方式:
SELECT * FROM t1 WHERE id 300000
SELECT * FROM t1 WHERE id NOT IN (300000)
SELECT * FROM t1 WHERE id >299999 AND id
在执行计划中可以明显看出,使用最后一种方式而不是前面两种方式进行查询。
网上是这么说的,但自己做的试验100W条数据,开销计划是一样的。
*********************************************
3. 函数运算不满足SARG形式,使得索引无法使用
例:下列SQL条件语句中的列都建有恰当的索引,但执行速度却非常慢:
select * from record where substring(card_no,1,4)=′5378′(13秒)
select * from record where amount/30
select * from record where convert(char(10),date,112)=′19991201′(10秒)
分析:
where子句中对列的任何操作结果都是在SQL运行时逐列计算得到的,因此它不得不进行全表扫描,而没有使用该列上面的索引;如果这些结果在查询编译时就能得到,那么就可以被SQL优化器优化,使用索引,避免表搜索,因此将SQL重写成下面这样:
select * from record where card_no like ′5378%′( select * from record where amount select * from record where date= ′1999/12/01′ (
你会发现SQL明显快很多
待测试.......
**********************************************
4.尽量不要对建立了索引的字段,作任何的直接处理
select * from employs where first_name + last_name ='beill cliton';
无法使用索引,改为:
select * from employee where
first_name = substr('beill cliton',1,instr('beill cliton',' ')-1)
and
last_name = substr('beill cliton',instr('beill cliton',' ')+1)
则可以使用索引
***********************************************
5.不同类型的索引效能是不一样的,应尽可能先使用效能高的
比如:数字类型的索引查找效率高于字符串类型,定长字符串char,nchar的索引效率高于变长字符串varchar,nvarchar的索引。
应该将
where username='张三' and age>20
改进为
where age>20 and username='张三'
注意:此处,SQL的查询分析优化功能可以做到自动重排条件顺序,但还是建议预先手工排列好。
**************************************************
6.某些情况下IN 的作用与OR 相当 ,且都不能充分利用索引
例:表stuff有200000行,id_no上有非群集索引,请看下面这个SQL:
select count(*) from stuff where id_no in(′0′,′1′) (23秒)
我们期望它会根据每个or子句分别查找,再将结果相加,这样可以利用id_no上的索引;但实际上,它却采用了"OR策略",即先取出满足每个or子句的行,存入临时数据库的工作表中,再建立唯一索引以去掉重复行,最后从这个临时表中计算结果。因此,实际过程没有利用id_no 上索引,并且完成时间还要受tempdb数据库性能的影响。
实践证明,表的行数越多,工作表的性能就越差,当stuff有620000行时,执行时间会非常长!如果确定不同的条件不会产生大量重复值,还不如将or子句分开:
select count(*) from stuff where id_no=′0′
select count(*) from stuff where id_no=′1′
得到两个结果,再用union作一次加法合算。因为每句都使用了索引,执行时间会比较短,
select count(*) from stuff where id_no=′0′
union
select count(*) from stuff where id_no=′1′
从实践效果来看,使用union在通常情况下比用or的效率要高的多,而exist关键字和in关键字在用法上类似,性能上也类似,都会产生全表扫描,效率比较低下,根据未经验证的说法,exist可能比in要快些。
***************************************************
7.使用变通的方法提高查询效率
like关键字支持通配符匹配,但这种匹配特别耗时。例如:select * from customer where zipcode like “21_ _ _”,即使在zipcode字段上已建立了索引,在这种情况下也可能还是采用全表扫描方式。如果把语句改为:select * from customer where zipcode >“21000”,在执行查询时就会利用索引,大大提高速度。但这种变通是有限制的,不应引起业务意义上的损失,对于邮政编码而言,zipcode like “21_ _ _” 和 zipcode >“21000” 意义是完全一致的。
*********************************************************人各有志,但富贵在天,人生允许彷徨,但不允许蹉跎.
8.order by按聚集索引列排序效率最高
排序是较耗时的操作,应尽量简化或避免对大型表进行排序,如缩小排序的列的范围,只在有索引的列上排序等等。
我们来看:(gid是主键,fariqi是聚合索引列)
select top 10000 gid,fariqi,reader,title from tgongwen
用时:196 毫秒。 扫描计数 1,逻辑读 289 次,物理读 1 次,预读 1527 次。
select top 10000 gid,fariqi,reader,title from tgongwen order by gid asc
用时:4720毫秒。 扫描计数 1,逻辑读 41956 次,物理读 0 次,预读 1287 次。
select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc
用时:4736毫秒。 扫描计数 1,逻辑读 55350 次,物理读 10 次,预读 775 次。
select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi asc
用时:173毫秒。 扫描计数 1,逻辑读 290 次,物理读 0 次,预读 0 次。
select top 10000 gid,fariqi,reader,title from tgongwen order by fariqi desc
用时:156毫秒。 扫描计数 1,逻辑读 289 次,物理读 0 次,预读 0 次。
同时,按照某个字段进行排序的时候,无论是正序还是倒序,速度是基本相当的。
********************************************************
9.关于节省数据查询系统开销方面的措施
(1)使用TOP尽量减少取出的数据量
(2)字段提取要按照“需多少、提多少”的原则,避免“select *”
字段大小越大,数目越多,select所耗费的资源就越多,比如取int类型的字段就会比取char的快很多。我们每少提取一个字段,数据的提取速度就会有相应的提升。提升的幅度根据舍弃的字段的大小来判断
(3)count(*) 与 count(字段) 方法比较
用count(*)和用 count(主键)的速度是相当的,而count(*)却比其他任何除主键以外的字段汇总速度要快,而且字段越长,汇总速度就越慢。如果用 count(*), SQL SERVER会自动查找最小字段来汇总。当然,如果您直接写count(主键)将会来的更直接些
(4)有嵌套查询时,尽可能在内层过滤掉数据
如果一个列同时在主查询和where子句中出现,很可能当主查询中的列值改变之后,子查询必须重新查询一次。而且查询嵌套层次越多,效率越低,因此应当尽量避免子查询。如果子查询不可避免,那么要在子查询中过滤掉尽可能多的行
(5)多表关联查询时,需注意表顺序,并尽可能早的过滤掉数据
在使用Join进行多表关联查询时候,应该使用系统开销最小的方案。连接条件要充份考虑带有索引的表、行数多的表,并注意优化表顺序;说的简单一点,就是尽可能早的将之后要做关联的数据量降下来。
一般情况下,sqlserver 会对表的连接作出自动优化。例如:
select name,no from A
join B on A. id=B.id
join C on C.id=A.id
where name='wang'
尽管A表在From中先列出,然后才是B,最后才是C。但sql server可能会首先使用c表。它的选择原则是相对于该查询限制为单行或少数几行,就可以减少在其他表中查找的总数据量。绝大多数情况下,sql server 会作出最优的选择,但如果你发觉某个复杂的联结查询速度比预计的要慢,就可以使用SET FORCEPLAN语句强制sql server按照表出现顺序使用表。如上例加上:SET FORCEPLAN ON…….SET FORCEPLAN OFF 表的执行顺序将会按照你所写的顺序执行。在查询分析器中查看2种执行效率,从而选择表的连接顺序。SET FORCEPLAN的缺点是只能在存储过程中使用

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Title: In-depth discussion of the importance and examples of establishing link files in Linux. In the Linux operating system, link files are a very useful concept. It can help users better organize and manage data in the file system and improve file accessibility. Accessibility and flexibility. Understanding how to create link files in Linux is crucial for system administrators and developers. This article will delve into the importance of establishing link files in Linux and demonstrate its usage and role through specific code examples. 1.What is

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

MDF file is a common database file format and it is one of the main files of Microsoft SQL Server database. In database management systems, MDF files are used to save the main data of the database, including tables, indexes, stored procedures, etc. Creating an MDF file is one of the key steps in creating a database. Some common methods will be introduced below. Using SQLServerManagementStudio(SSMS)SQLServerManag

Select the plus button on the homepage, then select Start a group chat, check the contacts you want to create a group, and then complete. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: WeChat 8.0.20 Analysis 1 First open WeChat and click the plus button in the upper right corner of the homepage. 2 Next, click the option to initiate a group chat in the pop-up window. 3Finally, check the contacts you want to create a group on the page and click Finish. Supplement: What is WeChat group chat? 1 WeChat chat group is a multi-person chat and communication network platform developed by Tencent. We can use the Internet to quickly transmit voice messages, short videos, high-definition pictures and text content. You can also use WeChat to communicate with friends in more colorful forms such as short messages, mobile MMS, etc.

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

A year has passed since the release of Win11 system, and many people have been wondering whether it is recommended to upgrade to Win11 in 2022. In fact, if the system we are currently using feels good and we are not experiencing any problems, upgrading is not necessary. Answer: It is not recommended to upgrade to win11 in 2022, because now win11 does not have much improvement compared to win11. If we like the new interface and settings of Win11, we might as well download it and give it a try. 1. Now there is no difference in software compatibility between win11 and win10. What can be used on win11 can also be used on win10. 2. If we are used to the operation of win10, we may still not be used to using win11, and many functions cannot be found. 3. For example

PHP Error Handling: Best Practices and Recommendations Error handling is a very important task when writing PHP code. If errors are not handled correctly, it can lead to vulnerabilities and security issues in your application. At the same time, good error handling also helps improve the maintainability and scalability of the code. This article will introduce some best practices and recommendations for PHP error handling and provide some code examples. Using Exception Handling In PHP, exceptions are a mechanism used to handle runtime errors. By using exceptions, errors can be
