SQL table with quot;listquot; entry vs SQL table with a row for each entry(带有“列表的 SQL 表条目 vs SQL 表,每个条目都有一行)
问题描述
我有一个 Sqlite
表,其中每一行的形式为:
I have a Sqlite
table where each row is the form of:
value, "a,b,c,d,e,f,g,h,i,j", value3, value4
在我的查询查找值对的地方,%b%
使用 LIKE
.就查询时间而言,使用一个每行都是一个排列的表是否更有效:
Where my queries look for a pairing of value, %b%
using LIKE
. Is it more efficient in terms of query time to instead have a table where each row is a permutation:
value, a, value3, value4
...
value, j, value3, value4
这样我的查询现在是 value,b 这次使用 = 运算符.
So that my queries are now value,b using the = operator this time.
请注意,我感兴趣的用例目前有大约 10,000 行,其中每个列表"元素平均有 8 个条目.我标记了 Sqlite
和 MySQL
因为我对带有 MySQL
和 Sqlite
的数据库都有类似的问题.
As a note, the use case I'm interested in has ~10,000 rows currently where each "list" element has on average 8 entries. I tagged both Sqlite
and MySQL
because I have a similar problem for both a db with MySQL
and Sqlite
.
推荐答案
我的查询在其中查找值对,%b% 使用 LIKE.是吗在查询时间方面更有效,而不是有一个表每一行都是一个排列:
Where my queries look for a pairing of value, %b% using LIKE. Is it more efficient in terms of query time to instead have a table where each row is a permutation:
绝对的.因为 LIKE '%something%' 类型查询 不能使用索引.所以你的查找会很慢.如果这还不够,您几乎可以使用 RDBMS 反模式.更多详情:正在存储数据库列中的分隔列表真的那么糟糕吗?
Most definitely. Because LIKE '%something%' type queries cannot use indexes. So you look ups are going to be very slow. If that's not enough you are using pretty much an RDBMS anti pattern. more details here: Is storing a delimited list in a database column really that bad?
将CSV"列分解为单独的列后,您可以通过规范化数据库来进一步处理.
once you break up your 'CSV' column into separate columns, you can take it still further by normalizing the database.
这篇关于带有“列表"的 SQL 表条目 vs SQL 表,每个条目都有一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有“列表"的 SQL 表条目 vs SQL 表,每个条目都有一行


基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01