0

我有这个查询大约需要 40-50 秒才能加载

SELECT
  members.latitude,
  members.longitude,
  members.mem_id
FROM members
WHERE profile_type != "C"
    AND (gender = 'm'
          OR gender = 'f')
    AND members.mem_id IN(SELECT DISTINCT
                            n2.mem_id
                          FROM network n1,
                            network n2
                          WHERE n1.frd_id = n2.mem_id
                              AND n1.mem_id = '48'
                              AND n2.frd_id = '48')
ORDER BY profilenam

有包含用户朋友的网络表

请帮忙..

4

3 回答 3

3

这很慢,因为使用的子查询在没有使用派生表的子查询的情况下尝试它:

SELECT latitude, longitude, a.mem_id
FROM members a
     INNER JOIN (SELECT DISTINCT n2.mem_id AS mem_id
                 FROM network n1,
                      network n2
                 WHERE n1.frd_id = n2.mem_id
                       AND n1.mem_id = '48'
                       AND n2.frd_id = '48'
                ) b
                ON a.mem_id = b.mem_id
WHERE profile_type != "C"
      AND (gender = 'm'
           OR gender = 'f')
ORDER BY profilenam;

还要在表上添加适当的索引并用于EXPLAIN EXTENDED检查索引使用情况:

ALTER TABLE network ADD KEY ix1(mem_id);
ALTER TABLE network ADD KEY ix1(frd_id);
于 2012-07-25T11:24:44.227 回答
0

将 IN 子句替换为 EXISTS。

IN 集合中只允许 1000 个项目

在(items_num=1000)

于 2012-07-25T11:29:32.297 回答
0

IN 子句只有在结果集很小的情况下才真正适合。对于更大的结果集,请考虑重写查询。过滤器(由 IN 子句生成)是一项昂贵的数据库操作。

于 2012-07-25T11:26:33.640 回答