我有这个应该是正确语法的 MySQL:
select c.cat_id,c.cat_name as cat_name,
c.cat_desc, c.cat_image, mi.filename,
l.link_id, l.user_id, l.address,l.city,
l.country,l.link_created,l.link_desc,
l.email,l.fax,l.link_hits, l.link_modified,
l.link_name,l.postcode, l.price,l.link_rating,
l.state,l.telephone,l.link_votes,
l.website, l.link_id, l.link_visited, cf.value
from j25_mt_cats as c,
j25_mt_links as l
LEFT OUTER JOIN j25_mt_cfvalues AS cf ON (cf.link_id = l.link_id),
j25_mt_images AS mi,
j25_mt_cl as cl
UNION ALL
select c.cat_id,c.cat_name as cat_name,
c.cat_desc, c.cat_image, mi.filename,
l.link_id, l.user_id, l.address,l.city,
l.country,l.link_created,l.link_desc,
l.email,l.fax,l.link_hits, l.link_modified,
l.link_name,l.postcode, l.price,l.link_rating,
l.state,l.telephone,l.link_votes,
l.website, l.link_id, l.link_visited, cf.value
FROM j25_mt_cats as c,
j25_mt_links as l
RIGHT OUTER JOIN j25_mt_cfvalues AS cf ON cf.link_id = l.link_id,
j25_mt_images AS mi,
j25_mt_cl as cl
where cf.cf_id = 40 and cl.link_id = l.link_id
AND mi.link_id = l.link_id AND mi.ordering < 2
AND c.cat_id = cl.cat_id and c.cat_published = 1
AND c.cat_approved = 1 and l.link_published = 1 and l.link_approved = 1
AND cf.link_id IS NULL;
该查询在 tmp 目录中占用了 3GB+ 并最终超时。我在这里遗漏了一些东西,如何提高效率?我的目标只是添加到现有查询中以从附加表 (j25_mt_cfvalues) 中获取值。
explain
:
+----+--------------+------------+-------+---------------+---------+---------+--------------------------+------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+--------------------------+------+------------------+
| 1 | PRIMARY | mi | ALL | NULL | NULL | NULL | NULL | 165 | |
| 1 | PRIMARY | c | ALL | NULL | NULL | NULL | NULL | 301 | |
| 1 | PRIMARY | l | ALL | NULL | NULL | NULL | NULL | 2139 | |
| 1 | PRIMARY | cf | ref | link_id | link_id | 4 | db_table.l.link_id | 2 | |
| 1 | PRIMARY | cl | index | NULL | PRIMARY | 4 | NULL | 2742 | Using index |
| 2 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE |
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+-------+---------------+---------+---------+--------------------------+------+------------------+
j25_mt_cats 模式:
CREATE TABLE
j25_mt_cats
(cat_id
int(11) NOT NULL auto_increment,cat_name
varchar(255) NOT NULL,alias
varchar(255) NOT NULL,title
varchar(255) NOT NULL,cat_desc
text NOT NULL,cat_parent
int(11) NOT NULL default '0',cat_links
int(11 ) NOT NULL 默认 '0',cat_cats
int(11) NOT NULL 默认 '0',cat_featured
tinyint(4) NOT NULL 默认 '0',cat_image
varchar(255) NOT NULL,cat_published
tinyint(4) NOT NULL 默认 '0',cat_created
datetime NOT NULL 默认值 '0000-00-00 00:00:00',cat_approved
tinyint(4) NOT NULL 默认值 '0',cat_template
varchar(255) NOT NULL 默认值 '',cat_usemainindex
tinyint(4) NOT NULL 默认值 '0',cat_allow_submission
tinyint(4 ) NOT NULL 默认为 '1',cat_show_listings
tinyint(3) unsigned NOT NULL default '1',metakey
text NOT NULL,metadesc
text NOT NULL,ordering
int(11) NOT NULL default '0',lft
int(11) NOT NULL default '0',rgt
int(11) NOT NULL default ' 0', PRIMARY KEY (cat_id
), KEYcat_id
(cat_id
,cat_published
,cat_approved
), KEYcat_parent
(cat_parent
,cat_published
,cat_approved
,cat_cats
,cat_links
), KEYdtree
(cat_published
,cat_approved
), KEYlft_rgt
(lft
,rgt
), KEYfunc_getPathWay
(lft
,rgt
,cat_id
,cat_parent
), KEYalias
(alias
) ) ENGINE=MyISAM AUTO_INCREMENT=3851 默认字符集=utf8 |
j25_mt_links 架构:
CREATE TABLE
j25_mt_links
(link_id
int(11) NOT NULL auto_increment,link_name
varchar(255) NOT NULL,alias
varchar(255) NOT NULL,link_desc
mediumtext NOT NULL,user_id
int(11) NOT NULL default '0',link_hits
int(11) NOT NULL default '0' ,link_votes
int(11) NOT NULL 默认'0',link_rating
decimal(7,6) unsigned NOT NULL 默认'0.000000',link_featured
smallint(6) NOT NULL 默认'0',link_published
tinyint(4) NOT NULL 默认'0',link_approved
int( 4) NOT NULL 默认 '0',link_template
varchar(255) NOT NULL,attribs
text NOT NULL,metakey
text NOT NULL,metadesc
text NOT NULL,internal_notes
text NOT NULL,ordering
int(11) NOT NULL default '0',link_created
datetime NOT NULL 默认 '0000-00-00 00:00:00',publish_up
datetime NOT NULL 默认 '0000-00-00 00:00:00',publish_down
datetime NOT NULL 默认 '0000-00-00 00:00:00' ,link_modified
datetime NOT NULL default '0000-00-00 00:00:00',link_visited
int(11) NOT NULL default '0',address
varchar(255) NOT NULL,city
varchar(255) NOT NULL,state
varchar(255) NOT NULL,country
varchar(255) NOT NULL,postcode
varchar(255) NOT NULL,telephone
varchar(255) NOT NULL,fax
varchar(255) NOT NULL,website
varchar(255) NOT NULL,price
double(9,2) NOT NULL默认 '0.00',lat
float(10,6) NOT NULL COMMENT '纬度',lng
float(10, 6) NOT NULL COMMENT '经度',zoom
tinyint(3) unsigned NOT NULL COMMENT '地图'的缩放级别', PRIMARY KEY (link_id
), KEYlink_rating
(link_rating
), KEYlink_votes
(link_votes
), KEYlink_name
(link_name
), KEYpublishing
(link_published
,link_approved
,publish_up
,publish_down
), KEYcount_listfeatured
(link_published
,link_approved
,link_featured
,publish_up
,publish_down
,link_id
), KEYcount_viewowner
(link_published
,link_approved
,user_id
,publish_up
,publish_down
), KEYmylisting
(user_id
,link_id
), FULLTEXT KEYlink_name_desc
(link_name
,link_desc
) ) ENGINE=MyISAM AUTO_INCREMENT=3229 DEFAULT CHARSET=utf8 |
j25_mt_cfvalues 模式:
CREATE TABLE
j25_mt_cfvalues
(id
int(11) NOT NULL auto_increment,cf_id
int(11) NOT NULL,link_id
int(11) NOT NULL,value
mediumtext NOT NULL,attachment
int(10) unsigned NOT NULL default '0',counter
int(11) NOT NULL default '0 ', 主键 (id
), 键cf_id
(cf_id
,link_id
), 键link_id
(link_id
), 键value
(value
(8))) ENGINE=MyISAM AUTO_INCREMENT=20876 默认字符集=utf8 |