日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

特定场景下SQL的优化

發(fā)布時間:2025/7/14 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 特定场景下SQL的优化 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.大表的數(shù)據(jù)修改最好分批處理。

1000萬行的記錄表中刪除更新100萬行記錄,一次只刪除或更新5000行數(shù)據(jù)。每批處理完成后,暫停幾秒中,進(jìn)行同步處理。

2.如何修改大表的表結(jié)構(gòu)。

對表的列的字段類型進(jìn)行修改,改變字段寬度時還是會鎖表,無法解決主從數(shù)據(jù)庫延遲的問題。

解決辦法:

1.創(chuàng)建一個新表。

2.在老表上創(chuàng)建觸發(fā)器同步老表數(shù)據(jù)到新表。

3.同步老表數(shù)據(jù)到新表。

4.刪除老表。

5.將新表重新命名為老表。

可以使用命令,完成上面的工作:

pt-online-schema-change –alter=”modify c varchar(150) not null default ‘’” –user=root –password=root d=sakia, t=表名 –charset=utf8 –execute

3.優(yōu)化not in 和 <> 的查詢

例子:

select customer_id ,firstname,email from customer where customer_id

not in (select customer_id from payment)

會多次查詢payment 表,如果payment表記錄很多效率將很低。

改寫后的sql

select a.customer_id ,a.firstname,a.email from customer a left join payment b

on a.customer_id=b.customer_id where b.customer_id is null;

4.對匯總表的優(yōu)化查詢

select count(1) from product_comment where product_id=999;

創(chuàng)建匯總表:

create table product_comment_cnt (product_id ,cnt int);

select sum(cnt) from (

select cnt from product_comment_cnt where product_id=999 union all

select count(1) from product_comment where product_id =999 and timestr>date(now())

) a

每天定時更新匯總表,再加上當(dāng)天的數(shù)據(jù)。

?

轉(zhuǎn)載于:https://www.cnblogs.com/yg_zhang/p/5936034.html

總結(jié)

以上是生活随笔為你收集整理的特定场景下SQL的优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。