i working on big data base contains >1 million records, using below query full out records based on criteria slow though index primary key.
sql:
select distinct title, productid, catid, description, producturl, regularprice, saleprice, imageurl, merid, created_at, updated_at, name, link, source products p, advertisers (title '%headphone%' or description '%headphone%') , catid=22 , regularprice not null , p.merid = a.advertid
can me on how optimize query?
thanks,
edit:
table structures:
create table `products` ( `id` int(10) unsigned not null auto_increment primary key, `productid` varchar(50) collate utf8_unicode_ci not null unique, `catid` varchar(50) collate utf8_unicode_ci not null, `title` text collate utf8_unicode_ci, `description` longtext collate utf8_unicode_ci, `producturl` text collate utf8_unicode_ci not null, `regularprice` double(8,2) default null, `saleprice` double(8,2) not null, `imageurl` text collate utf8_unicode_ci not null, `merid` varchar(50) collate utf8_unicode_ci not null unique, `created_at` timestamp not null default current_timestamp, `updated_at` timestamp not null default current_timestamp ) engine=innodb default charset=utf8 collate=utf8_unicode_ci; create table `advertisers` ( `id` int(10) unsigned not null primary key, `advertid` varchar(50) collate utf8_unicode_ci not null unique, `name` text collate utf8_unicode_ci not null, `link` text collate utf8_unicode_ci not null, `logo` text collate utf8_unicode_ci not null, `source` varchar(10) collate utf8_unicode_ci not null ) engine=innodb default charset=utf8 collate=utf8_unicode_ci;
explain:
mysql> explain extended select distinct title, productid, catid, description, producturl, regularprice, saleprice, imageurl, merid, created_at, updated_at, name, link, source products p, advertisers (title '%headphone%' or description '%headphone%') , catid=22 , regularprice not null , p.merid = a.advertid; +----+-------------+-------+------------+--------+---------------------+----------+---------+-------------------+--------+----------+------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | | +----+-------------+-------+------------+--------+---------------------+----------+---------+-------------------+--------+----------+------------------------------+ | 1 | simple | p | null | | merid | null | null | null | 682242 | 1.89 | using where; using temporary | | 1 | simple | | null | eq_ref | advertid,advertid_2 | advertid | 152 | datafeeds.p.merid | 1 | 100.00 | null | +----+-------------+-------+------------+--------+---------------------+----------+---------+-------------------+--------+----------+------------------------------+ 2 rows in set, 2 warnings (0.00 sec)
check index in join key merid products , advertid advertisers
select distinct title , productid , catid , description , producturl , regularprice , saleprice , imageurl , merid , created_at , updated_at , name , link , source products p inner join advertisers on p.merid = a.advertid (title '%headphone%' or description '%headphone%') , catid=22 , regularprice not null
coulb useful composite index on (probably ) more selective columns column
(catid, merid)
regularprice, title , description due use of , si not null don't should useful in composite index
Comments
Post a Comment