Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

mysql - SQL table with "list" entry vs SQL table with a row for each entry

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

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

So that my queries are now value,b using the = operator this time.

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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?

once you break up your 'CSV' column into separate columns, you can take it still further by normalizing the database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...