是否有更快的方法来查询特定条目的数据库而不是其ID?(Is there a faster way to query a database for a specific entry than by it

编程入门 行业动态 更新时间:2024-10-26 13:27:02
是否有更快的方法来查询特定条目的数据库而不是其ID?(Is there a faster way to query a database for a specific entry than by its ID?)

如果您有一个100,000,000,000条记录的数据库,并且您知道需要输入51,293,128,345,那么有什么比它快

SELECT * FROM table WHERE entry="51293128345"

如果你确切地知道你需要什么以及它是什么行,那么数据库是否仍然需要扫描所有先前的记录才能到达那个记录。

If you have a database of 100,000,000,000 records and you know you need entry 51,293,128,345 is there anything faster than

SELECT * FROM table WHERE entry="51293128345"

If you know exactly what you need and what row it is, does the database still need to scan through all preceding records to reach that one.

最满意答案

简单回答:在具有大量记录的SQL数据库中获取单个记录的最快方法是在密集索引列上使用where条件 - where myindexedcolumn = 1234567 。


在这种情况下,我收集您的列名称entry只是一个通用术语,但如果entry没有索引,并且您需要查询该列,那么您需要考虑以下事项:

在entry上添加索引(正如@therewillbesnacks所提到的,索引改进了选择,代价是更新/插入的额外成本)。 但是,如果entry不是唯一的并且包含大量重复项,则db查询优化器可能仍会忽略它并扫描整个表。

如果可能的话,查询索引列以及entry - 例如

WHERE myindexedcolumn = 1234 AND entry = "51293128345"

另一个考虑因素是使用完全不同的数据库类型,其中条目是所需记录的直接路径,例如Redis等(从未使用过,但我听过很好的事情)。 这不是一个建议,但如果您真的需要查询具有1000亿条记录的表格,您可能需要调查其他开箱即用的优化。

Simple answer: The fastest way to get a single record in an SQL database with a large number of records would be using a where condition on a dense indexed column - where myindexedcolumn = 1234567.


I gather your column name entry is just a generalised term in this case, but if entry does not have an index and that is the column you need to query on, then you have some considerations:

Add an index on entry (as @therewillbesnacks mentioned, indexes improve the select at the expense of an added cost to updates/inserts). However, if entry isn't unique and and contains large number of duplicates, then the db query optimiser may still ignore it and scan the whole table.

If possible query on an indexed column as well as entry - e.g.

WHERE myindexedcolumn = 1234 AND entry = "51293128345"

Another consideration is using a completely different database type where entry was a direct path to the required record e.g. Redis or the like (Never used it but I have heard good things). This isn't a suggestion as such, but if you seriously have to query tables with 100 billion records, you may want to investigate additional out of the box optimisation.

更多推荐

本文发布于:2023-08-05 21:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目的   更快   方法来   而不是   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!