欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
mysql count(*) count(val) count(1)比較
今天上網(wǎng),看到一種說(shuō)法如下(僅作記錄,不做評論):

如果表中沒(méi)有主鍵,那么count(1)比count(*)快
如果有主鍵,那么count(主鍵,聯(lián)合主鍵)比count(*)快
如果表中只有一個(gè)字段,count(*)最快

再摘一篇類(lèi)似的英文:http://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/

Looking at how people are using COUNT(*) and COUNT(col) it looks like most of them think they are synonyms and just using what they happen to like, while there is substantial difference in performance and even query result.
Lets look at the following series of examples:

PLAIN TEXT
SQL:
          CREATE TABLE `fact` (
            `i` int(10) UNSIGNED NOT NULL,
            `val` int(11) DEFAULT NULL,
            `val2` int(10) UNSIGNED NOT NULL,
            KEY `i` (`i`)
          ) ENGINE=MyISAM DEFAULT CHARSET=latin1


          mysql> SELECT count(*) FROM fact;
          +----------+
          | count(*) |
          +----------+
          |  7340032 |
          +----------+
          1 row IN SET (0.00 sec)


          mysql> SELECT count(val) FROM fact;
          +------------+
          | count(val) |
          +------------+
          |    7216582 |
          +------------+
          1 row IN SET (1.17 sec)


          mysql> SELECT count(val2) FROM fact;
          +-------------+
          | count(val2) |
          +-------------+
          |     7340032 |
          +-------------+
        • 1 row IN SET (0.00 sec)



As this is MYISAM table MySQL has cached number of rows in this table. This is why it is able to instantly answer COUNT(*) and
COUNT(val2) queries, but not COUNT(val). Why ? Because val column is not defined as NOT NULL there can be some NULL values in it and so MySQL have to perform table scan to find out. This is also why result is different for the second query.
So COUNT(*) and COUNT(col) queries not only could have substantial performance performance differences but also ask different question.
MySQL Optimizer does good job in this case doing full table scan only if it is needed because column can be NULL.
Now lets try few more queries:
PLAIN TEXT
SQL:
          mysql> SELECT count(*) FROM fact WHERE i<10000;
          +----------+
          | count(*) |
          +----------+
          |   733444 |
          +----------+
          1 row IN SET (0.40 sec)

          mysql> EXPLAIN SELECT count(*) FROM fact WHERE i<10000 \G
          *************************** 1. row ***************************
                     id: 1
            select_type: SIMPLE
                  TABLE: fact
                   type: range
          possible_keys: i
                    KEY: i
                key_len: 4
                    ref: NULL
                   rows: 691619
                  Extra: USING WHERE; USING INDEX
          1 row IN SET (0.00 sec)


          mysql> SELECT count(val) FROM fact WHERE i<10000;
          +------------+
          | count(val) |
          +------------+
          |     720934 |
          +------------+
          1 row IN SET (1.29 sec)

          mysql> EXPLAIN SELECT count(val) FROM fact WHERE i<10000 \G
          *************************** 1. row ***************************
                     id: 1
            select_type: SIMPLE
                  TABLE: fact
                   type: range
          possible_keys: i
                    KEY: i
                key_len: 4
                    ref: NULL
                   rows: 691619
                  Extra: USING WHERE
          1 row IN SET (0.00 sec)

          mysql> SELECT count(val2) FROM fact WHERE i<10000;
          +-------------+
          | count(val2) |
          +-------------+
          |      733444 |
          +-------------+
          1 row IN SET (1.30 sec)

          mysql> EXPLAIN SELECT count(val2) FROM fact WHERE i<10000 \G
          *************************** 1. row ***************************
                     id: 1
            select_type: SIMPLE
                  TABLE: fact
                   type: range
          possible_keys: i
                    KEY: i
                key_len: 4
                    ref: NULL
                   rows: 691619
                  Extra: USING WHERE
        • 1 row IN SET (0.00 sec)



As you can see even if you have where clause performance for count(*) and count(col) can be significantly different. In fact this example shows just 3 times performance difference because all data fits in memory, for IO bound workloads you frequently can see 10 and even 100 times performance difference in this case.
The thing is count(*) query can use covering index even while count(col) can't. Of course you can extend index to be (i,val) and get query to be index covered again but I would use this workaround only if you can't change the query (ie it is third party application) or in case column name is in the query for reason, and you really need count of non-NULL values.
It is worth to note in this case MySQL Optimizer does not do too good job optimizing the query. One could notice (val2) column is not null so count(val2) is same as count(*) and so the query could be run as index covered query. It does not and both queries have to perform row reads in this case.
PLAIN TEXT
SQL:
          mysql> ALTER TABLE fact DROP KEY i, ADD KEY(i,val);
          Query OK, 7340032 rows affected (37.15 sec)
          Records: 7340032  Duplicates: 0  Warnings: 0


          mysql> SELECT count(val) FROM fact WHERE i<10000;
          +------------+
          | count(val) |
          +------------+
          |     720934 |
          +------------+
        • 1 row IN SET (0.78 sec)



As you can see extending index helps in this case but it makes query about 2 times slower compared to count(*) one. This is probably because index becomes about two times longer in this case.
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
? MySQL數據庫InnoDB存儲引擎在線(xiàn)加字段實(shí)現原理詳解
MySQL中InnoDB引擎對索引的擴展
PHP+Ajax實(shí)現后臺文章快速排序
MySQL count(*) 空表為何會(huì )很慢
php 購物車(chē)的例子
基于php和mysql的簡(jiǎn)單的dao類(lèi)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久