MySQL查詢效率問題

2021-08-08 23:40:57 字數 1306 閱讀 8168

最近在使用python在mysql中讀寫資料時遇到了,兩個程式插入查詢語句格式相同,卻效率相差約百倍的問題。
原因是mysql欄位型別為字串,使用字串資料條件查詢比使用數值型資料效率高的很多,個人估算有百倍,
而我兩個程式就使用了兩種資料型別,花了兩天才找出效率慢的問題。後來去搜了一下相關問題,看到有人做出了
**對比,效率對比很直觀。
教訓就是,雖然資料庫可以對程式中的資料格式進行隱式轉換,但會帶來巨大的效率差異,所以以後對資料庫
進行查詢操作時,欄位是什麼型別,所用的查詢資料格式也應該是這種型別。
下面是我的程式:
程式1:
predictions是個生成器,裡面資料是

數值型

格式為:((432424,2343423,2.4343),(565645,656343,0.43354))
sql_update="update features_pos set ra=%s where src=%s and dist=%s "
for u,v,p in predictions:

counter=counter+1

db.update_record(sql_update,(p,u,v))

logging.warning("%s--############----%s-vs-%s############"% (counter ,u,v)

程式2:
db.get_fetchall 函式返回的是元祖型別,資料是unicode字串型
格式為:(,))
sql_update="update features_pos set cn=%s where src=%s and dist=%s "

counter = 0

for item in db.get_fetchall(query_src_dist):

counter = counter + 1

db.update_record(sql_update,(cn,item["src"],item["dist"]))

logging.warning("[%s]############----%s-vs-%s############"% (counter,item["src"],item["dist"]))

mysql 遞迴查詢效率 mysql 遞迴查詢

set stemp set stempchd cast id as char 轉化資料格式 while stempchd is not null do 開始迴圈 set stemp concat stemp,stempchd 字串拼接 select group concat id into stem...

mysql查詢效率低下的原因

用perl插入資料到mysql發現查詢某一ip是否在ip表中存在耗時特別多,如下 sql checkip select ip id from ip where ip address ip address sth dbh prepare sql checkip sth execute while da...

mysql 高效率查詢背景

說起mysql查詢效率問題,就無法繞開索引問題,而innodb索引是mysql儲存引擎中的重中之重,innodb索引包括 主鍵索引 聚集索引 也就是實際資料和主鍵資料儲存在一起的索引。innodb除了主鍵索引以外就是二級索引,二級索引葉子結點的資料區儲存的是主鍵,mysql每個資料頁面有16k大小,...