单列/多列索引,哪个更好?

发布于 2024-11-16 06:43:43 字数 169 浏览 8 评论 0原文

我有一个性能不佳的程序,其中有几个查询。

我已经确定了一些扫描临时表的临时表查询。我决定在临时表上添加索引以避免表扫描。我注意到在 where 子句中使用了临时表的多列。但是,我不确定是否应该将所有列包含在单个索引(复合索引)中,还是多个索引中,每个索引一列,以获得最大性能。

数据库是DB2

I have one poor performing procedure with couple of queries in it.

I have identified few temp table queries that does scanning of temp table. I decided to add index on temp table to avoid table scanning. I have noticed that there are multiple columns of temp table which are being used in where clause. However, I am not sure whether I should include all columns in single index (composite index) or multiple indexes with one column each index to gain the maximum performance.

Database is DB2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送舟行 2024-11-23 06:43:43

这在很大程度上取决于您的查询和表中的数据。根据经验,您应该只包含大大减少结果行的列。

如果第一个限制列的 where 子句已经删除了例如 90% 的行,而下一个限制列只会减少几百行,那么就不值得将资源包含在索引中。请始终记住,数据库引擎首先处理复合索引的第一列,然后继续处理下一个列。如果您的查询的列顺序不同,则索引可能会开始甚至减慢您的查询速度。

另外,如果您有大量数据并且使用多个索引列似乎值得,那么在某些情况下您可能希望拥有单独的索引并进行内部并行工作。如果您的服务器有空余的话,使用多个 CPU 运行并行索引查找可能会具有更好的性能。

This all depends greatly on your queries and the data on your table. As a rule of thumb you should include only the columns that reduce greatly the result rows.

If the where clause for first limiting column already drops for instance 90% of the rows and the next one would only reduce a few hundred rows anymore it is not worth the resources to include in the index. Always keep in mind that the database engine works first with the first column of composite index, and then proceeds to the next ones. If your queries have the columns in different order the index will potentially start even slowing your queries down.

Also, if you have a lot of data and using several indexed columns seems worth it you might in some cases want to have separate indexes and have intra-parallelism work. It is possible that running parallel index lookups using several CPUs has better performance - if your server has to spare.

陪你搞怪i 2024-11-23 06:43:43

对于 MySQL,可以使用多列索引来进行测试索引中所有列的查询,或者仅测试第一列、前两列、前三列等的查询。

如果在索引定义中以正确的顺序指定列,则单个复合索引可以加快对同一表的多种查询的速度。

假设您在表 test 上创建了 INDEX nameIdx (last_name,first_name)

因此,nameIdx 索引用于以下查询中的查找:

SELECT * FROM test WHERE last_name='Widenius';

SELECT * FROM test
  WHERE last_name='Widenius' AND first_name='Michael';

SELECT * FROM test
  WHERE last_name='Widenius'
  AND (first_name='Michael' OR first_name='Monty');

where as name nameIdx 不用于以下查询中的查找:

SELECT * FROM test WHERE first_name='Michael';

SELECT * FROM test
  WHERE last_name='Widenius' OR first_name='Michael';

有关更多详细信息,请参阅 URL

总结是,如果您按照索引顺序(从左到右)使用 where 子句中的列,那么它比单列索引更好

In case of MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on.

If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

Lets say that you have INDEX nameIdx (last_name,first_name) created on table test

Therefore, the nameIdx index is used for lookups in the following queries:

SELECT * FROM test WHERE last_name='Widenius';

SELECT * FROM test
  WHERE last_name='Widenius' AND first_name='Michael';

SELECT * FROM test
  WHERE last_name='Widenius'
  AND (first_name='Michael' OR first_name='Monty');

where as name nameIdx is not used for lookups in the following queries:

SELECT * FROM test WHERE first_name='Michael';

SELECT * FROM test
  WHERE last_name='Widenius' OR first_name='Michael';

for more detail refer URL

summary of this is if you are using columns in where clause as mentioned in index order (from left to right ) then it is better than single column index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文