sql效能優化(利用merge)

2021-08-13 20:04:06 字數 1367 閱讀 2453

業務背景:

當被查詢的表的資料量非常龐大的時候,你的乙個查詢將會非常的緩慢。為了提高 查詢效能。

1。利用merge 是做到sql集群,類似分表。但不是真正意義上的分表。

例如 user 表有 100 萬條資料。

create table if not exists `user1` (  

-> `id` int(11) not null auto_increment,

-> `name` varchar(50) default null,

-> `***` int(1) not null default '0',

-> primary key (`id`)

-> ) engine=myisam default charset=utf8 auto_increment=1 ;

query ok, 0 rows affected (0.05 sec)

mysql> create table if not exists `user2` (

-> `id` int(11) not null auto_increment,

-> `name` varchar(50) default null,

-> `***` int(1) not null default '0',

-> primary key (`id`)

-> ) engine=myisam default charset=utf8 auto_increment=1 ;

query ok, 0 rows affected (0.01 sec)

然後將兩張表整合在一起

create table if not exists `user` (  

-> `id` int(11) not null auto_increment,

-> `name` varchar(50) default null,

-> `***` int(1) not null default '0',

-> index(id)

-> ) type=merge union=(user1,user2) insert_method=last auto_increment=1 ;

query ok, 0 rows affected, 1 warning (0.00 sec)

有些人肯定會說分兩張表後,哪些資料sql怎麼辦?

這時把mysql檔案的語句改,在上一步建user表的時候,user表名與資料庫現有的user名保持一致。這樣就解決了現存資料的問題

當然,不是所有的mysql都能這麼操作。

效能優化 SQL優化

1.列型別盡量定義成數值型別,且長度盡可能短,如主鍵和外來鍵,型別字段等等 2.建立單列索引 3.根據需要建立多列聯合索引 當單個列過濾之後還有很多資料,那麼索引的效率將會比較低,即列的區分度較低,那麼如果在多個列上建立索引,那麼多個列的區分度就大多了,將會有顯著的效率提高。4.根據業務場景建立覆蓋...

SQL效能優化

postgre資料表資料比較多的情況下,使用模糊查詢效能很差,但是使用函式反而快了,返回資料一致,有點不解 warning表2688133條資料,warning message表6954788條資料 這個sql執行老半天都沒反映,耗時169904 ms select count 1 from war...

sql效能優化

任何平台的sql開發者都有自身的困惑,似乎他們一直糾纏在do while迴圈裡,這個迴圈讓他們不斷地重複同樣的錯誤。這是因為資料庫的發展依然不夠成熟。當然,商們也在不斷進步,但是他們還是需要處理更嚴重的問題。併發性,資源管理,空間管理和速度依然制約著sql開發者對開發平台的選擇。我並不期望sql開發...