[英]Optimize SQL-query in big database?
詢問
SELECT *
FROM user_ip_tmp
WHERE too = 'http://example.com/'
AND contry != 'CN'
AND contry != 'TW'
ORDER BY id DESC
LIMIT 50
MySQL返回:
Showing rows 0 - 29 ( 50 total, Query took 11.9276 sec) [id: 3452538 - 3448824]
如果我刪除ID DESC的ORDER BY
Showing rows 0 - 29 ( 50 total, Query took 0.0033 sec)
說明計划:
計數
SELECT count( * )
FROM user_ip_tmp
使用的數據庫示例
CREATE TABLE IF NOT EXISTS `user_ip_tmp` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`ip` varchar(20) NOT NULL,
`dataip` bigint(20) NOT NULL,
`ref` text NOT NULL,
`click` int(20) NOT NULL,
`code` varchar(17) NOT NULL,
`too` text NOT NULL,
`checkopen` varchar(17) NOT NULL,
`contry` text NOT NULL,
`vOperation` text NOT NULL,
`vBrowser` text NOT NULL,
`iconOperation` text NOT NULL,
`iconBrowser` text NOT NULL,
PRIMARY KEY (`id`),
KEY `ip` (`dataip`),
KEY `ip` (`checkopen`),
KEY `ip` (`code`),
KEY `ip` (`too`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5426268 ;
我想要正確的方法來查詢和優化數據庫以ORDER BY id DESC
知道一些有關數據分布的信息將很有趣。 您能否將以下查詢的結果添加到您的帖子中? (無需圖片,純文本即可)。
SELECT count(*) FROM user_ip_tmp WHERE too = 'http://example.com/' AND contry != 'CN' AND contry != 'TW';
SELECT count(*) FROM user_ip_tmp WHERE too = 'http://example.com/';
另外,您可以測試這種替代方法的性能嗎? 編輯:子查詢的別名
SELECT sub.* FROM
(SELECT *
FROM user_ip_tmp
WHERE too = 'http://example.com/'
AND contry != 'CN'
AND contry != 'TW'
) sub
ORDER BY sub.id DESC
LIMIT 50
編輯如果可以選擇添加索引並嘗試使用索引,則可以嘗試使用其中一個(或同時嘗試兩個,看看哪個更好)
CREATE INDEX index_name ON `user_ip_tmp` (`too`, `id`);
CREATE INDEX index_name ON `user_ip_tmp` (`too`, `contry`, `id`);
您可以使用以下方法創建索引:
id, too and contry
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.