如何处理Sqlite中重复的列?通过使用压缩?

问题描述 投票:0回答:2

我知道(从这个问题的答案)Sqlite默认情况下不启用压缩。 是否可以启用它,或者这需要其他工具吗?情况如下:

我需要在 Sqlite 数据库中添加数百万行。该表包含一个

description
列(平均约 500 个字符),平均每个
description
由 40 行共享,如下所示:

id    name    othercolumn    description 
1     azefds  ...            This description will be the same for probably 40 rows
2     tsdyug  ...            This description will be the same for probably 40 rows
...
40    wxcqds  ...            This description will be the same for probably 40 rows
41    azeyui  ...            This one is unique
42    uiuotr  ...            This one will be shared by 60 rows
43    poipud  ...            This one will be shared by 60 rows
...
101   iuotyp  ...            This one will be shared by 60 rows
102   blaxwx  ...            Same description for the next 10 rows
103   sdhfjk  ...            Same description for the next 10 rows
...

问题:

  • 您会像这样插入行,并启用数据库的压缩算法吗? 优点:你不必处理2个表,查询时更容易。

  • 你会用2张桌子吗?

    id    name    othercolumn    descriptionid
    1     azefds  ...            1
    2     tsdyug  ...            1    
    ...
    40    wxcqds  ...            1
    41    azeyui  ...            2
    ...
    
    id    description
    1     This description will be the same for probably 40 rows
    2     This one is unique
    

    缺点:我们必须使用复杂的方法来检索它,而不是解决方案#1中的简单

    select id, name, description from mytable
    ,涉及2个表,可能还需要多个查询?或者也许可以在没有复杂查询的情况下做到这一点,但是用
    union
    merge
    或类似的东西进行巧妙的查询?

python sql sqlite compression
2个回答
2
投票

使用多个表不仅可以防止不一致,占用更少的空间,而且可能会更快,即使涉及多个/更复杂的查询(正是因为它涉及移动更少的数据)。您应该使用哪个取决于哪些特征对您来说最重要。

当您有 2 个表时检索结果的查询看起来像这样(这实际上只是两个表之间的联接):

select table1.id, table1.name, table1.othercolumn, table2.description
from table1, table2
where table1.descriptionid=table2.id

0
投票

这里是 ScottHunter 答案的一些 Python 插图代码:

import sqlite3

conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("CREATE TABLE mytable (id integer, name text, descriptionid integer)")
c.execute("CREATE TABLE descriptiontable (id integer, description text)")

c.execute('INSERT INTO mytable VALUES(1, "abcdef", 1)');
c.execute('INSERT INTO mytable VALUES(2, "ghijkl", 1)');
c.execute('INSERT INTO mytable VALUES(3, "iovxcd", 2)');
c.execute('INSERT INTO mytable VALUES(4, "zuirur", 1)');
c.execute('INSERT INTO descriptiontable VALUES(1, "Description1")');
c.execute('INSERT INTO descriptiontable VALUES(2, "Description2")');

c.execute('SELECT mytable.id, mytable.name, descriptiontable.description FROM mytable, descriptiontable WHERE mytable.descriptionid=descriptiontable.id');

print(c.fetchall())

#[(1, u'abcdef', u'Description1'),
# (2, u'ghijkl', u'Description1'), 
# (3, u'iovxcd', u'Description2'), 
# (4, u'zuirur', u'Description1')]

编辑:有观点:

import sqlite3
 
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("CREATE TABLE mytable (id integer, name text, descriptionid integer)")
c.execute("CREATE TABLE descriptiontable (id integer, description text)")
 
# Create the view
c.execute('CREATE VIEW mytable_with_desc AS SELECT mytable.id AS id, mytable.name AS name, descriptiontable.description AS description FROM mytable, descriptiontable WHERE mytable.descriptionid=descriptiontable.id')
 
c.execute('INSERT INTO mytable VALUES(1, "abcdef", 1)');
c.execute('INSERT INTO mytable VALUES(2, "ghijkl", 1)');
c.execute('INSERT INTO mytable VALUES(3, "iovxcd", 2)');
c.execute('INSERT INTO mytable VALUES(4, "zuirur", 1)');
c.execute('INSERT INTO descriptiontable VALUES(1, "Description1")');
c.execute('INSERT INTO descriptiontable VALUES(2, "Description2")');
 
# Use the view
c.execute('SELECT id, name, description FROM mytable_with_desc')
print(c.fetchall())
© www.soinside.com 2019 - 2024. All rights reserved.