简体   繁体   English

对MySQL InnoDB聚集索引使用组合主键是否好?

[英]is it good to use combined primary key for MySQL InnoDB clustered index?

I'm trying to build an aggregated news/blog/forum website from multiple sources. 我正在尝试从多个来源构建一个汇总的新闻/博客/论坛网站。

Because most queries are likely to be within same time periods for written_time column, I'm thinking about taking advantage of clustered index ordered by written_time. 因为大多数查询可能在针对write_time列的相同时间段内,所以我正在考虑利用writered_time排序的聚集索引。

But because it is not unique, i'm thinking about making primary key with unique id like: 但是因为它不是唯一的,所以我正在考虑制作具有唯一ID的主键,例如:

(written_time, site_id, article_id)

I think it would require somewhat bigger space but it's much better than having secondary indices. 我认为这将需要更大的空间,但是比拥有二级索引要好得多。 Is it a good approach to make cluster index like this if I would like to take advantage of query results which have proximity about written-time? 如果我想利用与书面时间相近的查询结果,是否可以使集群索引像这样好?

Here are some use case scenarios: 以下是一些用例场景:

  • the website's main page shows recent aggregated articles 该网站的主页显示了最近汇总的文章

    eg SELECT .. FROM written_time >= datetime_1weeksago 例如SELECT .. FROM written_time >= datetime_1weeksago

  • Users can see articles of every board for specific time periods 用户可以查看特定时间段内每个委员会的文章

    eg SELECT .. FROM written_time >= datetime1 AND written_time < datetime2 例如SELECT .. FROM written_time >= datetime1 AND written_time < datetime2

  • Users can see articles which contains specific keyword for specific time chunk (eg 201207), user can narrow search criteria down into some selected sites, search traffic volume is not high, Going to use full-text engine, frequent search result is cached by keyword*time_chunk. 用户可以看到包含针对特定时间段(例如201207)的特定关键字的文章,用户可以将搜索条件缩小到某些选定的站点中,搜索流量不高,将要使用全文引擎,频繁的搜索结果将按关键字进行缓存* time_chunk。

    eg SELECT .. FROM written_time >= '2012-07-01' AND written_time < '2012-08-01' + keyword search using full-text engine 例如SELECT .. FROM written_time >= '2012-07-01' AND written_time < '2012-08-01' + keyword search using full-text engine

    eg SELECT .. FROM written_time >= '2012-07-01' AND written_time < '2012-08-01' AND site_id IN (1,3,5,7,9) + keyword search using full-text engine 例如: SELECT .. FROM written_time >= '2012-07-01' AND written_time < '2012-08-01' AND site_id IN (1,3,5,7,9) + keyword search using full-text engine

  • Background crawler fetches large number of articles in two ways and appends in two directions: (this is why i want to make clustered index with written_time) 后台搜寻器以两种方式获取大量文章,并在两个方向进行追加:(这就是为什么我想使用书面时间来创建聚集索引的原因)

    1. periodcally crawls and updates recent articles (appends entries with newer written_time) 定期检索和更新最近的文章(将条目添加为更新的writed_time)

    2. scrawls and archives old articles (appends entries with written_time) 搜寻和存档旧文章(将条目附加在writeed_time后面)

  • huge amount of articles from number of highly-active news/blog/forum 来自大量活跃新闻/博客/论坛的大量文章

出于时间和空间的原因,最好为InnoDB表使用单个基于AUTO_INCREMENT的主键,因为InnoDB将PRIMARY KEY值存储在所有其他索引中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM