带有“列表"的 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.
相关文章