SQL語句優(yōu)化原則,sql語句優(yōu)化
Jun 13, 2016 am 08:57 AMSQL語句優(yōu)化原則,sql語句優(yōu)化
?
處理百萬級以上的數(shù)據(jù)提高查詢速度的方法:
1.應(yīng)盡量避免在?where?子句中使用!=或操作符,否則將引擎放棄使用索引而進(jìn)行全表掃描。
?2.對查詢進(jìn)行優(yōu)化,應(yīng)盡量避免全表掃描,首先應(yīng)考慮在?where?及?order?by?涉及的列上建立索引。
?3.應(yīng)盡量避免在?where?子句中對字段進(jìn)行?null?值判斷,否則將導(dǎo)致引擎放棄使用索引而進(jìn)行全表掃描,
如:
?????select?id?from?t?where?num?is?null
?????可以在num上設(shè)置默認(rèn)值0,確保表中num列沒有null值,然后這樣查詢:
?????select?id?from?t?where?num=0
?4.應(yīng)盡量避免在?where?子句中使用?or?來連接條件,否則將導(dǎo)致引擎放棄使用索引而進(jìn)行全表掃描,如:
?????select?id?from?t?where?num=10?or?num=20
?????可以這樣查詢:
?????select?id?from?t?where?num=10
?????union?all
?????select?id?from?t?where?num=20
?5.下面的查詢也將導(dǎo)致全表掃描:(不能前置百分號)
?????select?id?from?t?where?name?like?‘%abc%’
????若要提高效率,可以考慮全文檢索。
?6.in?和?not?in?也要慎用,否則會(huì)導(dǎo)致全表掃描,如:
?????select?id?from?t?where?num?in(1,2,3)
?????對于連續(xù)的數(shù)值,能用?between?就不要用?in?了:
?????select?id?from?t?where?num?between?1?and?3
?select?'13891030091'?phone??union?select?'13992085916'?…………??UNION??SELECT?'13619100234'?)?b?
??on??a.Phone=b.phone
--替代下面??很多數(shù)據(jù)隔開的時(shí)候
in('13891030091','13992085916','13619100234'…………)
?
7.如果在?where?子句中使用參數(shù),也會(huì)導(dǎo)致全表掃描。因?yàn)镾QL只有在運(yùn)行時(shí)才會(huì)解析局部變量,但優(yōu)化程序不能將訪問計(jì)劃的選擇推遲到運(yùn)行時(shí);它必須在編譯時(shí)進(jìn)行選擇。然?而,如果在編譯時(shí)建立訪問計(jì)劃,變量的值還是未知的,因而無法作為索引選擇的輸入項(xiàng)。如下面語句將進(jìn)行全表掃描:
?????select?id?from?t?where?num=@num?????可以改為強(qiáng)制查詢使用索引:
?????select?id?from?t?with(index(索引名))?where?num=@num
?8.應(yīng)盡量避免在?where?子句中對字段進(jìn)行表達(dá)式操作,這將導(dǎo)致引擎放棄使用索引而進(jìn)行全表掃描。如:
?????select?id?from?t?where?num/2=100
?????應(yīng)改為:
?????select?id?from?t?where?num=100*2
?9.應(yīng)盡量避免在where子句中對字段進(jìn)行函數(shù)操作,這將導(dǎo)致引擎放棄使用索引而進(jìn)行全表掃描。如:
?????select?id?from?t?where?substring(name,1,3)=’abc’–name以abc開頭的id
?????select?id?from?t?where?datediff(day,createdate,’2005-11-30′)=0–’2005-11-30′生成的id
?????應(yīng)改為:
?????select?id?from?t?where?name?like?‘a(chǎn)bc%’
?????select?id?from?t?where?createdate>=’2005-11-30′?and?createdate2005-12-1′
?10.不要在?where?子句中的“=”左邊進(jìn)行函數(shù)、算術(shù)運(yùn)算或其他表達(dá)式運(yùn)算,否則系統(tǒng)將可能無法正確使用索引。
?11.在使用索引字段作為條件時(shí),如果該索引是復(fù)合索引,那么必須使用到該索引中的第一個(gè)字段作為條件時(shí)才能保證系統(tǒng)使用該索引,否則該索引將不會(huì)被使?用,并且應(yīng)盡可能的讓字段順序與索引順序相一致。
?12.不要寫一些沒有意義的查詢,如需要生成一個(gè)空表結(jié)構(gòu):
?????select?col1,col2?into?#t?from?t?where?1=0
?????這類代碼不會(huì)返回任何結(jié)果集,但是會(huì)消耗系統(tǒng)資源的,應(yīng)改成這樣:
?????create?table?#t(…)
?13.很多時(shí)候用?exists?代替?in?是一個(gè)好的選擇:
?????select?num?from?a?where?num?in(select?num?from?b)
?????用下面的語句替換:
?????select?num?from?a?where?exists(select?1?from?b?where?num=a.num)
?14.并不是所有索引對查詢都有效,SQL是根據(jù)表中數(shù)據(jù)來進(jìn)行查詢優(yōu)化的,當(dāng)索引列有大量數(shù)據(jù)重復(fù)時(shí),SQL查詢可能不會(huì)去利用索引,如一表中有字段?sex,male、female幾乎各一半,那么即使在sex上建了索引也對查詢效率起不了作用。
?15.索引并不是越多越好,索引固然可以提高相應(yīng)的?select?的效率,但同時(shí)也降低了?insert?及?update?的效率,因?yàn)?insert?或?update?時(shí)有可能會(huì)重建索引,所以怎樣建索引需要慎重考慮,視具體情況而定。一個(gè)表的索引數(shù)最好不要超過6個(gè),若太多則應(yīng)考慮一些不常使用到的列上建的索引是否有?必要。
?16.應(yīng)盡可能的避免更新?clustered?索引數(shù)據(jù)列,因?yàn)?clustered?索引數(shù)據(jù)列的順序就是表記錄的物理存儲(chǔ)順序,一旦該列值改變將導(dǎo)致整個(gè)表記錄的順序的調(diào)整,會(huì)耗費(fèi)相當(dāng)大的資源。若應(yīng)用系統(tǒng)需要頻繁更新?clustered?索引數(shù)據(jù)列,那么需要考慮是否應(yīng)將該索引建為?clustered?索引。
?17.盡量使用數(shù)字型字段,若只含數(shù)值信息的字段盡量不要設(shè)計(jì)為字符型,這會(huì)降低查詢和連接的性能,并會(huì)增加存儲(chǔ)開銷。這是因?yàn)橐嬖谔幚聿樵兒瓦B接時(shí)會(huì)?逐個(gè)比較字符串中每一個(gè)字符,而對于數(shù)字型而言只需要比較一次就夠了。
?18.盡可能的使用?varchar/nvarchar?代替?char/nchar?,因?yàn)槭紫茸冮L字段存儲(chǔ)空間小,可以節(jié)省存儲(chǔ)空間,其次對于查詢來說,在一個(gè)相對較小的字段內(nèi)搜索效率顯然要高些。
?19.任何地方都不要使用?select?*?from?t?,用具體的字段列表代替“*”,不要返回用不到的任何字段。
?20.盡量使用表變量來代替臨時(shí)表。如果表變量包含大量數(shù)據(jù),請注意索引非常有限(只有主鍵索引)。
?21.避免頻繁創(chuàng)建和刪除臨時(shí)表,以減少系統(tǒng)表資源的消耗。
?22.臨時(shí)表并不是不可使用,適當(dāng)?shù)厥褂盟鼈兛梢允鼓承├谈行?,例如,?dāng)需要重復(fù)引用大型表或常用表中的某個(gè)數(shù)據(jù)集時(shí)。但是,對于一次性事件,最好使?用導(dǎo)出表。
?23.在新建臨時(shí)表時(shí),如果一次性插入數(shù)據(jù)量很大,那么可以使用?select?into?代替?create?table,避免造成大量?log?,以提高速度;如果數(shù)據(jù)量不大,為了緩和系統(tǒng)表的資源,應(yīng)先create?table,然后insert。
?24.如果使用到了臨時(shí)表,在存儲(chǔ)過程的最后務(wù)必將所有的臨時(shí)表顯式刪除,先?truncate?table?,然后?drop?table?,這樣可以避免系統(tǒng)表的較長時(shí)間鎖定。
?25.盡量避免使用游標(biāo),因?yàn)橛螛?biāo)的效率較差,如果游標(biāo)操作的數(shù)據(jù)超過1萬行,那么就應(yīng)該考慮改寫。
?26.使用基于游標(biāo)的方法或臨時(shí)表方法之前,應(yīng)先尋找基于集的解決方案來解決問題,基于集的方法通常更有效。
?27.與臨時(shí)表一樣,游標(biāo)并不是不可使用。對小型數(shù)據(jù)集使用?FAST_FORWARD?游標(biāo)通常要優(yōu)于其他逐行處理方法,尤其是在必須引用幾個(gè)表才能獲得所需的數(shù)據(jù)時(shí)。在結(jié)果集中包括“合計(jì)”的例程通常要比使用游標(biāo)執(zhí)行的速度快。如果開發(fā)時(shí)?間允許,基于游標(biāo)的方法和基于集的方法都可以嘗試一下,看哪一種方法的效果更好。
?28.在所有的存儲(chǔ)過程和觸發(fā)器的開始處設(shè)置?SET?NOCOUNT?ON?,在結(jié)束時(shí)設(shè)置?SET?NOCOUNT?OFF?。無需在執(zhí)行存儲(chǔ)過程和觸發(fā)器的每個(gè)語句后向客戶端發(fā)送?DONE_IN_PROC?消息。
?29.盡量避免向客戶端返回大數(shù)據(jù)量,若數(shù)據(jù)量過大,應(yīng)該考慮相應(yīng)需求是否合理。
?30.盡量避免大事務(wù)操作,提高系統(tǒng)并發(fā)能力。?
文章來源:http://www.cnblogs.com/pepcod/archive/2013/01/01/2913496.html
?
?
優(yōu)化sql的文章參考:
?
http://www.cnblogs.com/ATree/archive/2011/02/13/sql_optimize_1.html
http://blog.csdn.net/csh624366188/article/details/8457749
http://www.iteye.com/problems/100945
http://blog.itpub.net/28389881/viewspace-1301549/? ? ? ? ? ? ? ?一個(gè)insert插入語句很慢的優(yōu)化?
?

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

JDBC...

When developing a project that requires parsing SQL statements, I encountered a tricky problem: how to efficiently parse MySQL's SQL statements and extract the key information. After trying many methods, I found that the greenlion/php-sql-parser library can perfectly solve my needs.

phpMyAdmin can be used to create databases in PHP projects. The specific steps are as follows: Log in to phpMyAdmin and click the "New" button. Enter the name of the database you want to create, and note that it complies with the MySQL naming rules. Set character sets, such as UTF-8, to avoid garbled problems.

This article introduces a detailed tutorial on joining three tables using SQL statements to guide readers step by step how to effectively correlate data in different tables. With examples and detailed syntax explanations, this article will help you master the joining techniques of tables in SQL, so that you can efficiently retrieve associated information from the database.

phpMyAdmin is not just a database management tool, it can give you a deep understanding of MySQL and improve programming skills. Core functions include CRUD and SQL query execution, and it is crucial to understand the principles of SQL statements. Advanced tips include exporting/importing data and permission management, requiring a deep security understanding. Potential issues include SQL injection, and the solution is parameterized queries and backups. Performance optimization involves SQL statement optimization and index usage. Best practices emphasize code specifications, security practices, and regular backups.

Detailed explanation of PostgreSQL database resource monitoring scheme under CentOS system This article introduces a variety of methods to monitor PostgreSQL database resources on CentOS system, helping you to discover and solve potential performance problems in a timely manner. 1. Use PostgreSQL built-in tools and views PostgreSQL comes with rich tools and views, which can be directly used for performance and status monitoring: pg_stat_activity: View the currently active connection and query information. pg_stat_statements: Collect SQL statement statistics and analyze query performance bottlenecks. pg_stat_database: provides database-level statistics, such as transaction count, cache hit
