需要有关表创建和使用文本主键的最佳实践建议

发布于 2024-11-03 07:21:12 字数 811 浏览 0 评论 0原文

我正在创建一个 MySQL MyISAM (需要全文搜索)表。

  1. column_1 - 包含一个 TEXT 主键(数据将是一个 64 位编码字符串)
  2. column_2 - 引用另一个表并用于连接
  3. column_3 - 另一个 TEXT 列,使用 MATCH 为搜索建立索引
  4. ...

该表可能保存数十亿条记录时间。

column_1 主搜索将在主键列上执行,如下所示。例如,

SELECT * FROM table WHERE column_1 = 123;

column_2 主搜索将按如下方式执行:

SELECT * FROM table_1 
JOIN table_2 ON ( table_1.column_2 == table_2.id );

column_3 主搜索将按如下方式执行:

SELECT column_3, MATCH ( column_3 )
AGAINST ( 'TOKEN' ) AS score
FROM table_1;

我想就需要设置的索引类型以及任何其他听起来相关的建议寻求建议。

提前致谢。

PS

我是否正确地认为,如果您进行搜索,例如,

SELECT * FROM table WHERE id = 1; (where id column is not indexed)

对大量数据库的搜索会比对列建立索引慢?

I'm creating a MySQL MyISAM (Full textual searches are needed) table.

  1. column_1 - contains a TEXT primary key (Data will be a 64 bit encoded string)
  2. column_2 - references another table and is used for joins
  3. column_3 - another TEXT column indexed for searches using MATCH
  4. ...

The table is likely to hold billions of records over time.

column_1 main search would be performed on the primary key column as follows. e.g.

SELECT * FROM table WHERE column_1 = 123;

column_2 main search would be performed as follows:

SELECT * FROM table_1 
JOIN table_2 ON ( table_1.column_2 == table_2.id );

column_3 main search would be performed as follows:

SELECT column_3, MATCH ( column_3 )
AGAINST ( 'TOKEN' ) AS score
FROM table_1;

I would like to take advice on the sort of indexes I would need to setup and any other advice that sounds relevant.

Thanks in advance.

P.S

Am I right in thinking that if you do a search e.g.

SELECT * FROM table WHERE id = 1; (where id column is not indexed)

The search on a substantial db would be slower than if the column was indexed?

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

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

发布评论

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

评论(1

靑春怀旧 2024-11-10 07:21:12
  1. 由于 PRIMARY KEY 已建立索引,因此第一个查询不需要更多索引。
  2. table_2.id 应该被索引(如果是文本字段,则在该字段的前几个字节上建立索引)。 table_1.column_2 不需要建立索引,因为您没有对该字段进行选择。
  3. column_3 需要 FULL TEXT 索引。

你的最终假设是正确的。该索引由专门为搜索而定制的数据结构组成,其中列作为键,指向正确行的指针作为值。对非索引字段的搜索将需要全表扫描(使数据库检查表的每一行)。

  1. You don't need any more indices for the first query since the PRIMARY KEY is indexed already.
  2. table_2.id should be indexed (if a text field, make the indexed on the first few bytes of the field). table_1.column_2 does not need to be indexed since you do no selection on that field.
  3. column_3 needs a FULL TEXT index.

You are right in your final assumption. The index is made up by a data structure specifically tailored for searching in, with the column as key and a pointer to the correct row as the value. A search on a non-indexed field will require a full table scan (making the db examine every row of the table).

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