Mysql 查詢優化 劉益長

2022-10-10 03:00:12 字數 3897 閱讀 8783

#建立表

create

table

ifnot

exists

cnt(

id int,

name

varchar(10

), age

int,

tel

varchar(10));

#建立儲存過程

delimiter $

create

procedure

cnt()

begin

#定義乙個循壞變數

declare i int

default0;

while(i<

1000

) do

begin

select

i;

set i=i+1;

insert

into cnt(id,name)values

(i,"zhang");

end;

endwhile

;end

$delimiter ;

#呼叫儲存過程

call cnt();

#查詢語句a

select

count(*) from cnt where id >5;

#查詢語句b

select (select

count(*) from cnt) -

count(*) from cnt where id <=

5;

語句當行數超過11行的時候需要掃瞄的行數比b語句要多, b語句掃瞄了6行,此種情況下,b語句比a語句更有效率。當沒有where語句的時候直接select count(*) from world.city這樣會更快,因為mysql總是知道表的行數。

例如float和int、char和varchar、binary和varbinary是不相容的。資料型別的不相容可能使優化器無法執行一些本來可以進行的優化操作。在程式中,保證在實現功能的基礎上,儘量減少對資料庫的訪問次數;通過搜尋引數,儘量減少對錶的訪問行數,最小化結果集,從而減輕網路負擔;能夠分開的操作盡量分開處理,提高每次的響應速度;在資料視窗使用sql時,盡量把使用的索引放在選擇的首列;演算法的結構盡量簡單;在查詢時,不要過多地使用萬用字元如 select * from t1語句,要用到幾列就選擇幾列如:select col1,col2 from t1;在可能的情況下盡量限制盡量結果集行數如:select top 300 col1,col2,col3 from t1,因為某些情況下使用者是不需要那麼多的資料的。不要在應用中使用資料庫游標,游標是非常有用的工具,但比使用常規的、面向集的sql語句需要更大的開銷;按照特定順序提取資料的查詢

#資料不相容會拉低效率

insert

into cnt(id)values(12.3);

盡量避免在where子句中對字段進行函式或表示式操作,這將導致引擎放棄使用索引而進行全表掃瞄

#索引列進行運算會讓索引失效

create

index index_age on

cnt(age);

select

*from cnt where age >18;

select

*from cnt where age *

2>36;

create

index index_age on

cnt(id);

select

*from cnt where id >

10000

;select

*from cnt where id *

2>

20000;

因為這會使系統無法使用索引,而只能直接搜尋表中的資料。例如: select id from employee where id != 「b%」

優化器將無法通過索引來確定將要命中的行數,因此需要搜尋該錶的所有行。在in語句中能用exists語句代替的就用exists.

#建立表

create

table

ifnot

exists

emp(

id intprimary

keyauto_increment,

name

varchar(10

), age

int,

tel

varchar(10));

#建立儲存過程

delimiter $

create

procedure

emp()

begin

#定義乙個循壞變數

declare i int

default0;

while(i<

1000

) do

begin

select

i;

set i=i+1;

insert

into emp(name,age)values

("zhang",i);

end;

endwhile

;end

$delimiter ;

# 呼叫儲存過程

call emp();

select

*from

emp;

# 避免的情況

!=, <>, is

null, is

notnull, in, not

inselect

count(*) from emp where age in (select age from emp where id >

100);

select

count(*) from emp where

exists (select age from emp where id >

100);

一部分開發人員和資料庫管理人員喜歡把包含數值資訊的字段 設計為字元型,這會降低查詢和連線的效能,並會增加儲存開銷。

這是因為引擎在處理查詢和連線回逐個比較字串中每乙個字元,而對於數字型而言只需要比較一次就夠了。

# 盡量使用數字型字段

create

table

ifnot

exists t1 (c1 int,c2 int

);create

table

ifnot

exists t2 (c1 int,c2 int);

# 使用where >0;

select

sum(t1.c1) from t1 where (select

count(*) from t2 where t2.c2=t1.c2 >0);

# 使用exists

select

sum(t1.c1) from t1 where

exists (select

count(*) from t2 where t2.c2=t1.c2);

# between

是連續的範圍,使用索引

select

*from emp where age between

100and

200;

# in不連續的範圍,不能使用索引

select

*from emp where age in (100,150,200

);# distinct去重:重複的資料取乙個

select

distinct name from

emp;

# group

by分組

select name from emp group

by name;

查詢優化(MySQL優化查詢)

關聯查詢太多join 設計缺陷或不得已的需求 資料庫伺服器調優及各個引數設定不適當 緩衝 執行緒數等 慢查詢日誌 找出執行速度慢的sql語句 慢查詢的開啟並捕獲 explain 慢sql分析 show profile查詢sql在mysql伺服器裡面的執行細節和生命週期情況 sql資料庫伺服器的引數調...

mysql統計查詢優化 Mysql查詢優化

效能涉及的層面很多,但是在操作層面,主要有表結構設計優化 索引優化和查詢優化 查詢的生命週期大致可以分為,從客戶端 到服務端 在伺服器上解析 生成執行計畫 執行 返回結果給客戶端 sql執行流程 具體優化技巧 1.消除外連線 2.消除子查詢 盡量用join代替子查詢,雖說mysql查詢優化器會進行優...

MySQL優化 查詢優化

在每乙個消耗大量時間的查詢中,都能看到一些不必要的額外操作 某些操作被額外地重複了很多次 某些操作執行得太慢等。優化查詢的目的就是減少和消除這些操作所花費的時間。查詢效能低下最基本的原因是訪問的資料太多。所以需要考慮是否向資料庫請求了不需要的資料 1 多表關聯時,或獲取單錶資料時,盡量避免不加思考地...