Mysql 使用WHERE子查询改进SQL
我有一个SQL,虽然有点慢,但运行良好。理想情况下,我们希望在where子句中有额外的子查询,类似这样的内容,并且country_iso=ukMysql 使用WHERE子查询改进SQL,mysql,sql,Mysql,Sql,我有一个SQL,虽然有点慢,但运行良好。理想情况下,我们希望在where子句中有额外的子查询,类似这样的内容,并且country_iso=uk SELECT c.content_name, c.content_telephone, c.content_address1 (SELECT w.weblink_url FROM weblinks w WHERE w.content_id = c.content_id ORDER BY w.weblink_ordering, w.weblink_
SELECT c.content_name, c.content_telephone, c.content_address1
(SELECT w.weblink_url FROM weblinks w WHERE w.content_id = c.content_id ORDER BY w.weblink_ordering, w.weblink_id ASC LIMIT 1)
AS content_url,
(SELECT country_name FROM countries WHERE country_id = c.country_id LIMIT 1)
AS country_name,
(SELECT country_iso FROM countries WHERE country_id = c.country_id LIMIT 1)
AS country_iso
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
WHERE cc.category_id = 7
AND (SELECT country_iso FROM countries WHERE country_id = c.country_id LIMIT 1) = 'uk'
ORDER BY cc.catcontent_ordering ASC, c.content_name ASC
这个问题不适合你的需要吗?基本上,使用连接获取国家,而不是子选择
SELECT
c.content_name,
c.content_telephone,
c.content_address1,
countries.country_name,
countries.country_iso,
(
SELECT w.weblink_url
FROM weblinks w
WHERE w.content_id = c.content_id
ORDER BY w.weblink_ordering, w.weblink_id ASC
LIMIT 1
) content_url
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
JOIN countries ON countries.country_id = c.country_id
WHERE cc.category_id = 7
AND countries.country_iso = 'uk'
ORDER BY cc.catcontent_ordering ASC, c.content_name ASC
这个问题不适合你的需要吗?基本上,使用连接获取国家,而不是子选择
SELECT
c.content_name,
c.content_telephone,
c.content_address1,
countries.country_name,
countries.country_iso,
(
SELECT w.weblink_url
FROM weblinks w
WHERE w.content_id = c.content_id
ORDER BY w.weblink_ordering, w.weblink_id ASC
LIMIT 1
) content_url
FROM catcontents cc
LEFT JOIN content c ON c.content_id = cc.content_id
JOIN countries ON countries.country_id = c.country_id
WHERE cc.category_id = 7
AND countries.country_iso = 'uk'
ORDER BY cc.catcontent_ordering ASC, c.content_name ASC
我想问的是,是否有一种方法可以不必查询countries表3次,从而提高查询的性能他不喜欢/不想更改查询的这一部分:并从country_id=c的国家中选择country_iso。country_id LIMIT 1='uk'使用外部联接代替子查询是否有可能存在多个特定国家/地区id的国家/地区行?因为除非MySQL提供一些奇怪的专业化,无法保证单独子查询返回的行在没有ORDER by子句的情况下是同一行。country_id是主键,在countries表中设置为unique。我想问的是,是否有一种方法不必查询countries表3次,从而提高查询的性能他不喜欢/不想更改查询的这一部分:并从country\u id=c的国家中选择country\u iso。country\u id LIMIT 1='uk'使用外部联接而不是子查询对于特定的country\u id,是否有可能在国家中有多行?因为除非MySQL提供一些奇怪的专门化,否则无法保证单独子查询返回的行在没有按子句排序的情况下是同一行。country_id是主键,在countries表中设置为unique。+1比我快了一分钟,但是,如果有多个国家/地区为'uk'的记录,则会出现一个问题,因为不知道数据是一个潜在的问题,可以通过将a限制1和='uk'限制放在子查询中来解决。事实上,我只是根据列和表名进行了假设。但是,大家都知道,假设每个人都是傻瓜\:+1比我快了一分钟,但是如果有多个国家/地区iso=‘英国’的记录,就有问题了,需要指出的是,不知道数据是一个潜在的问题,可以通过将a limit 1和='uk'限制放在子查询连接中来解决。事实上,我只是根据列和表名进行了假设。但是,大家都知道,假想会让每个人都成为傻瓜\: