我有一個450000行充滿新聞的表。 表架構是這樣的:Mysql索引配置
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL auto_increment,
`cat_id` int(11) NOT NULL,
`title` tinytext NOT NULL,
`content` text NOT NULL,
`date` int(11) NOT NULL,
`readcount` int(11) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `cat_id` (`cat_id`),
KEY `cat_id_2` (`cat_id`,`id`),
KEY `cat_id_date` (`cat_id`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin5 AUTO_INCREMENT=462679 ;
當我運行像下面的SQL命令採取一些新聞的頁面「X」分類頁面的花費超過15秒,如果x超過100:
select * news where cat_id='4' order by id desc limit 150000,10;
說明顯示,其使用「where」和索引「cat_id_2」
在寫這個問題我也查了更簡單的SQL查詢這樣的,它也花了接近一分鐘:
select * from haberler order by id desc limit 40000,10;
如果SQL是類似以下它只需幾毫秒:
select * from haberler order by id desc limit 20,10;
我的my.cnf配置是這樣的:
skip-locking
skip-innodb
query_cache_limit=1M
query_cache_size=256M
query_cache_type=1
max_connections=30
interactive_timeout=600000
#wait_timeout=5
#connect_timeout=5
thread_cache_size=384
key_buffer=256M
join_buffer=4M
max_allowed_packet=16M
table_cache=1024
record_buffer=1M
sort_buffer_size=64M
read_buffer_size=16M
max_connect_errors=10
# Try number of CPU's*2 for thread_concurrency
thread_concurrency=2
myisam_sort_buffer_size=128M
long_query_time = 1
log_slow_queries = /var/log/mysql/mysql-slow.log
max_heap_table_size=512M
該網站上的Core 2 Duo運行與2GB的RAM。 我認爲這個問題可能是由sort_buffer_size引起的,但我不確定。 在此先感謝。
也可能是您的索引,你有沒有考慮這方面? – 2009-10-23 11:58:00
您的問題存在一些不一致之處,請您將其編輯以使其更清楚? – 2009-10-23 12:03:01
它現在必須修好,表中使用的名字本來就是土耳其語,似乎我忘了將它們中的一些翻譯成英文。 對不起我的壞英語的方式。 – intacto 2009-10-23 12:45:57