国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術(shù)及軟件下載分享
分類導航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

香港云服务器
服務器之家 - 數(shù)據(jù)庫 - Mysql - MySQL數(shù)據(jù)庫中把int轉(zhuǎn)化varchar引發(fā)的慢查詢

MySQL數(shù)據(jù)庫中把int轉(zhuǎn)化varchar引發(fā)的慢查詢

2020-06-16 15:12mrr Mysql

這篇文章主要介紹了MySQL數(shù)據(jù)庫中把int轉(zhuǎn)化varchar引發(fā)的慢查詢 的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下

最近一周接連處理了2個由于int向varchar轉(zhuǎn)換無法使用索引,從而引發(fā)的慢查詢。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CREATE TABLE `appstat_day_prototype_201305` (
`day_key` date NOT NULL DEFAULT '1900-01-01',
`appkey` varchar(20) NOT NULL DEFAULT '',
`user_total` bigint(20) NOT NULL DEFAULT '0',
`user_activity` bigint(20) NOT NULL DEFAULT '0',
`times_total` bigint(20) NOT NULL DEFAULT '0',
`times_activity` bigint(20) NOT NULL DEFAULT '0',
`incr_login_daily` bigint(20) NOT NULL DEFAULT '0',
`unbind_total` bigint(20) NOT NULL DEFAULT '0',
`unbind_activitys` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`appkey`,`day_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
mysql> explain SELECT * from appstat_day_prototype_201305 where appkey = xxxxx and day_key between '2013-05-23' and '2013-05-30';
+----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
| 1 | SIMPLE | appstat_day_prototype_201305 | ALL | PRIMARY | NULL | NULL | NULL | 19285787 | Using where |
+----+-------------+------------------------------+------+---------------+------+---------+------+----------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from appstat_day_prototype_201305 where appkey = 'xxxxx' and day_key between '2013-05-23' and '2013-05-30';
+----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | appstat_day_prototype_201305 | range | PRIMARY | PRIMARY | 65 | NULL | 1 | Using where |
+----+-------------+------------------------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

從上面可以很明顯的看到由于appkey是varchar,而在where條件中不加'',會引發(fā)全表查詢,加了就可以用到索引,這掃描的行數(shù)可是天差地別,對于服務器的壓力和響應時間自然也是天差地別的。

我們再看另外一個例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*************************** 1. row ***************************
Table: poll_joined_151
Create Table: CREATE TABLE `poll_joined_151` (
`poll_id` bigint(11) NOT NULL,
`uid` bigint(11) NOT NULL,
`item_id` varchar(60) NOT NULL,
`add_time` int(11) NOT NULL DEFAULT '0',
`anonymous` tinyint(1) NOT NULL DEFAULT '0',
`sub_item` varchar(1200) NOT NULL DEFAULT '',
KEY `idx_poll_id_uid_add_time` (`poll_id`,`uid`,`add_time`),
KEY `idx_anonymous_id_addtime` (`anonymous`,`poll_id`,`add_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SELECT * FROM poll_joined_151 WHERE poll_id = '2348993' AND anonymous =0 ORDER BY add_time DESC LIMIT 0 , 3
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: poll_joined_151
type: ref
possible_keys: idx_poll_id_uid_add_time,idx_anonymous_id_addtime
key: idx_anonymous_id_addtime
key_len: 9
ref: const,const
rows: 30240
Extra: Using where

從上面的例子看,雖然poll_id的類型為bigint,但是SQL中添加了'',但是這個語句仍然用到了索引,雖然掃描行數(shù)也不少,但是能用到索引就是好SQL。

那么一個小小的''為什么會有這么大的影響呢?根本原因是因為MySQL在對文本類型和數(shù)字類型進行比較的時候會進行隱式的類型轉(zhuǎn)換。

以下是5.5官方手冊的說明:

?
1
2
3
4
5
6
7
8
9
10
11
If both arguments in a comparison operation are strings, they are compared as strings.
兩個參數(shù)都是字符串,會按照字符串來比較,不做類型轉(zhuǎn)換。
If both arguments are integers, they are compared as integers.
兩個參數(shù)都是整數(shù),按照整數(shù)來比較,不做類型轉(zhuǎn)換。
Hexadecimal values are treated as binary strings if not compared to a number.
十六進制的值和非數(shù)字做比較時,會被當做二進制串。
If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. Note that this is not done for the arguments to IN()! To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.
有一個參數(shù)是 TIMESTAMP 或 DATETIME,并且另外一個參數(shù)是常量,常量會被轉(zhuǎn)換為 timestamp
If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.
有一個參數(shù)是 decimal 類型,如果另外一個參數(shù)是 decimal 或者整數(shù),會將整數(shù)轉(zhuǎn)換為 decimal 后進行比較,如果另外一個參數(shù)是浮點數(shù),則會把 decimal 轉(zhuǎn)換為浮點數(shù)進行比較
In all other cases, the arguments are compared as floating-point (real) numbers.所有其他情況下,兩個參數(shù)都會被轉(zhuǎn)換為浮點數(shù)再進行比較

根據(jù)以上的說明,當where條件之后的值的類型和表結(jié)構(gòu)不一致的時候,MySQL會做隱式的類型轉(zhuǎn)換,都將其轉(zhuǎn)換為浮點數(shù)在比較。

對于第一種情況:

比如where string = 1;

需要將索引中的字符串轉(zhuǎn)換成浮點數(shù),但是由于'1',' 1','1a'都會比轉(zhuǎn)化成1,故MySQL無法使用索引只能進行全表掃描,故造成了慢查詢的產(chǎn)生。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
mysql> SELECT CAST(' 1' AS SIGNED)=1;
+-------------------------+
| CAST(' 1' AS SIGNED)=1 |
+-------------------------+
| 1 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT CAST(' 1a' AS SIGNED)=1;
+--------------------------+
| CAST(' 1a' AS SIGNED)=1 |
+--------------------------+
| 1 |
+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT CAST('1' AS SIGNED)=1;
+-----------------------+
| CAST('1' AS SIGNED)=1 |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.00 sec)

同時需要注意一點,由于都會轉(zhuǎn)換成浮點數(shù)進行比較,而浮點數(shù)只有53bit,故當超過最大值的時候,比較會出現(xiàn)問題。

對于第二種情況:

由于索引建立在int的基礎(chǔ)上,而將純數(shù)字的字符串可以百分百轉(zhuǎn)換成數(shù)字,故可以使用到索引,雖然也會進行一定的轉(zhuǎn)換,消耗一定的資源,但是最終仍然使用了索引,不會產(chǎn)生慢查詢。

?
1
2
3
4
5
6
7
mysql> select CAST( '30' as SIGNED) = 30;
+----------------------------+
| CAST( '30' as SIGNED) = 30 |
+----------------------------+
| 1 |
+----------------------------+
1 row in set (0.00 sec)

以上所述是小編給大家介紹的MySQL數(shù)據(jù)庫中把int轉(zhuǎn)化varchar引發(fā)的慢查詢  ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/billyxp/archive/2013/05/31/3110016.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
336
主站蜘蛛池模板: 国产精品99久久免费观看 | 免费视频一区 | 国产成人免费在线 | 网站色 | 日韩成人在线观看 | 亚洲视频在线免费观看 | 欧美一区二区三区在线看 | 日韩高清一区二区 | 久草视频网 | 国产精品高潮呻吟久久 | 91精品国产乱码久久久久久久久 | 鲁一鲁av| 欧美日韩精品在线 | 一级毛片黄 | 久草热线| 伊人伊人网 | 久久久在线免费观看 | 四虎永久在线观看 | 日韩国产| 五月天婷婷社区 | 1000部精品久久久久久久久 | 色视频在线免费观看 | 国产精品久久久久久久久久东京 | 蜜臀久久99精品久久久无需会员 | 精品综合久久 | 久久综合久色欧美综合狠狠 | 欧美成人精精品一区二区频 | 国产婷婷精品av在线 | 日韩视频一区二区三区 | 天天久久| 狠狠综合久久 | 午夜av影院| 免费在线观看一区二区 | 野狼在线社区2017入口 | 毛片com | 老妇60一区二区三区 | 中文一区 | 亚洲在线观看一区二区 | 91人人网| 免费黄色在线观看视频 | 国产精品一区二区三区在线 |