简体   繁体   中英

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 .

This query has complexity N . In order to make the search more efficient I can use col1 as index. In this case the same query should have complexity log(N) .

Now as far as I understood a column is made searchable in sqlalchemy setting it as a primary key.

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)?

EDIT: The example is written using sqlite but I am actually working with postgres .

Now as far as I understood a column is made searchable in sqlalchemy setting it as a primary key.

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 :

table = Table('example', metadata,
    Column('col1', Integer, index=True),
    Column('col2', Integer)
)

You can see in the log file the corresponding SQL:

INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_example_col1 ON example (col1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM