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 表,每个条目都有一行
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
