Here is some innodb table. It insert,update,query
very frequently.
tn
is product id
col
is product sub id
tindex
is product description.(store by each word)
date
is product add date stored by strototime('now')
CREATE TABLE IF NOT EXISTS `mytable` (
`tn` varchar(15) NOT NULL,
`col` smallint(2) NOT NULL,
`tindex` varchar(30) NOT NULL,
`date` int(10) NOT NULL,
KEY `date` (`date`),
KEY `tn` (`tn`),
KEY `tindex` (`tindex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
How I want to query some products which sub id contains words 'anticorrosive' and 'waterproof'
SELECT *
FROM mytable a
WHERE tindex = 'anticorrosive'
AND EXISTS (
SELECT 1
FROM mytable
WHERE tn = a.tn
AND col = a.col
AND tindex = 'waterproof'
)
ORDER BY DATE
LIMIT 10
full table is neally 12,700,000
lines, 1.1GB
,it cost 4.8356s
, explian the query get
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a ref tn,tindex tindex 92 const 1173 Using where; Using filesort
2 DEPENDENT SUBQUERY mytable ref tn,tindex tn 47 production.a.tn 161 Using where
And here is my.ini
,
[mysqld]
character_set_server=utf8
skip-external-locking
skip-networking
key_buffer = 256M
tmp_table_size = 128M
max_connections = 1024
wait_timeout=10
back_log = 2048
key_buffer_size = 256M
max_allowed_packet = 2M
table_cache = 2048
table_open_cache = 1024
sort_buffer_size = 8M
read_buffer_size = 4M
net_buffer_length = 92K
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 256M
thread_cache = 512
query_cache_size= 256M
bulk_insert_buffer_size = 192M
ft_min_word_len=2
innodb_buffer_pool_size = 512M
innodb_flush_log_at_trx_commit = 2
How to speed up the query? re-do the index? query code? or change some value in my.ini
UPDATE 1
I have tried change index kye, and my table still grow up, now it has neally 20mln
lines 2.2GB
ALTER TABLE mytable ADD UNIQUE INDEX (tn,col,tindex);
ALTER TABLE mytable ADD INDEX (date);
query 1
SELECT A.* FROM
(SELECT * FROM mytable WHERE tindex='words1') A
INNER JOIN
(SELECT tn,col FROM mytable WHERE tindex='words2') B
USING (tn,col) order by date limit 10
using 29.6278s
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 15674 Using temporary; Using filesort
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 1716 Using where; Using join buffer
3 DERIVED mytable index NULL date 5 NULL 13918039 Using where; Using index
2 DERIVED mytable index NULL date 5 NULL 13918039 Using where; Using index
query 2
SELECT * FROM mytable
WHERE tindex IN ('words1','words2')
GROUP BY tn,col HAVING COUNT(1)=2 order by date limit 10;
using 23.8711s
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE mytable index NULL date 5 NULL 19359399 Using where; Using index; Using temporary
(tindex, tn, col, date)