[英]searchable columns with sqlalchemy and postgres
I am a beginner with databases and I am not sure to understand them properly. 我是数据库的初学者,我不确定是否正确理解它们。
As far as I understand given a table with several columns I can make queries like: SELECT * FROM table WHERE col1>3
. 据我所知,给定一个包含多列的表,我可以进行如下查询:
SELECT * FROM table WHERE col1>3
。
This query has complexity N
. 该查询的复杂度为
N
In order to make the search more efficient I can use col1
as index. 为了使搜索更加有效,我可以使用
col1
作为索引。 In this case the same query should have complexity log(N)
. 在这种情况下,同一查询应具有复杂度
log(N)
。
Now as far as I understood a column is made searchable in sqlalchemy setting it as a primary key. 现在,据我了解,可以在sqlalchemy中将一列设置为主键来进行搜索。
If this is correct I do not understand why I am not able to set columns with duplicates as primary key. 如果这是正确的,我不明白为什么我不能将具有重复项的列设置为主键。
For example: 例如:
import sqlite3
from sqlalchemy import *
metadata = MetaData()
table = Table('example', metadata,
Column('col1', Integer, primary_key=True),
Column('col2', Integer))
engine = create_engine('sqlite:///:memory:', echo=True)
con = engine.connect()
table.create(engine, checkfirst=True)
data = [{'col1':1, 'col2':2}, {'col1':3, 'col2':4}, {'col1':3, 'col2':4}]
ins = table.insert().values(data)
con.execute(ins)
print list(con.execute("SELECT * FROM example"))
returns
IntegrityError: (sqlite3.IntegrityError) PRIMARY KEY must be unique [SQL: u'INSERT INTO example (col1, col2) VALUES (?, ?), (?, ?), (?, ?)'] [parameters: (1, 2, 3, 4, 3, 4)]
How can I make a non unique column searchable in log(N)? 如何使非唯一列在log(N)中可搜索?
EDIT: The example is written using sqlite but I am actually working with postgres . 编辑:该示例是使用sqlite编写的,但实际上我正在使用postgres。
Now as far as I understood a column is made searchable in sqlalchemy setting it as a primary key.
现在,据我了解,可以在sqlalchemy中将一列设置为主键来进行搜索。
A primary key column is automatically indexed, but you frequently need to index non-unique columns. 主键列会自动建立索引,但是您经常需要索引非唯一列。 You'd do this with the
index
keyword argument : 您可以使用
index
关键字参数来做到这一点:
table = Table('example', metadata,
Column('col1', Integer, index=True),
Column('col2', Integer)
)
You can see in the log file the corresponding SQL: 您可以在日志文件中看到相应的SQL:
INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_example_col1 ON example (col1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.