在WHERE子句中使用时,MySQL是否缓存子查询?

时间:2021-12-30 04:18:32

In the following query:

在以下查询:

SELECT column_a, column_b FROM table_a WHERE
    column_b IN (SELECT b_id FROM table_b)

Is the subquery SELECT b_id FROM table_b cached by the SQL parser, or would it be faster to do the query beforehand, save it as a variable (in PHP, for example), and then pass those values in as a CSV string?

子查询是从table_b中选择b_id,由SQL解析器缓存,还是更快地执行查询,将其保存为变量(例如PHP),然后将这些值作为CSV字符串传入?

e.g.

如。

SELECT column_a, column_b FROM table_a WHERE
    column_b IN (1,3,4,6,8,10,16,18)

1 个解决方案

#1


6  

Look into using EXPLAIN EXTENDED to fully illustrate the effects dealt on the subquery.

查看如何使用EXPLAIN EXTENDED来充分说明对子查询处理的效果。

For instance:

例如:

EXPLAIN EXTENDED
SELECT column_a, column_b FROM table_a WHERE
    column_b IN (SELECT b_id FROM table_b)

If they do not yield the caching results you wish, you may be interested in storing them either in memory (memcache, redis), on file (using PHP file libraries) or in a separate SQL cache itself.

如果它们不产生您希望的缓存结果,您可能会对将它们存储在内存(memcache, redis)、文件(使用PHP文件库)或单独的SQL缓存本身感兴趣。

#1


6  

Look into using EXPLAIN EXTENDED to fully illustrate the effects dealt on the subquery.

查看如何使用EXPLAIN EXTENDED来充分说明对子查询处理的效果。

For instance:

例如:

EXPLAIN EXTENDED
SELECT column_a, column_b FROM table_a WHERE
    column_b IN (SELECT b_id FROM table_b)

If they do not yield the caching results you wish, you may be interested in storing them either in memory (memcache, redis), on file (using PHP file libraries) or in a separate SQL cache itself.

如果它们不产生您希望的缓存结果,您可能会对将它们存储在内存(memcache, redis)、文件(使用PHP文件库)或单独的SQL缓存本身感兴趣。