【问题标题】:sqlalchemy add index to existing sqlite3 databasesqlalchemy 向现有 sqlite3 数据库添加索引
【发布时间】:2018-02-27 21:30:21
【问题描述】:

我已经用 pandas 创建了一个数据库:

import numpy as np                                                                                                                                                                                          
import sqlite3                                                                                                                                                                                              
import pandas as pd                                                                                                                                                                                         
import sqlite3                                                                                                                                                                                              
import sqlalchemy                                                                                                                                                                                           
from sqlalchemy import create_engine                                                                                                                                                                        
from sqlalchemy.orm import sessionmaker                                                                                                                                                                     

df = pd.DataFrame(np.random.normal(0, 1, (10, 2)), columns=['A', 'B'])                                                                                                                                      

path = 'sqlite:////home/username/Desktop/example.db'                                                                                                                                                        

engine = create_engine(path, echo=False)                                                                                                                                                                    

df.to_sql('flows', engine, if_exists='append', index=False)                                                                                                                                                 

# This is only to show I am able to read the database                                                                                                                                                                                                            
df_l = pd.read_sql("SELECT * FROM flows WHERE A>0 AND B<0", engine)                                                                                                                                         

现在我想为数据库添加一个或多个索引。 在这种情况下,我想首先只制作A 列,然后制作两个列索引。

我该怎么做?

如果可能的话,我想要一个只使用 SqlAlchemy 的解决方案,这样它就可以独立于数据库的选择。

【问题讨论】:

  • 通常会使用alembic 等迁移工具来处理数据库迁移,但不确定它是否适合您的用例。
  • 我的用例很简单。不需要太多工作(花时间学习新工具)的任何程序都是好的。

标签: python pandas sqlite sqlalchemy


【解决方案1】:

您应该使用反射来获取 pandas 为您创建的表。

参考:

SQLAlchemy Reflecting Database Objects

可以指示 Table 对象从 对应的数据库模式对象已经存在于 数据库。这个过程称为反射。在最简单的情况下 您只需要指定表名、MetaData 对象和 自动加载=真标志。如果 MetaData 没有被持久绑定,也 添加 autoload_with 参数:

你可以试试这个:

meta = sqlalchemy.MetaData()
meta.reflect(bind=engine)
flows = meta.tables['flows']
# alternative of retrieving the table from meta:
#flows = sqlalchemy.Table('flows', meta, autoload=True, autoload_with=engine)

my_index = sqlalchemy.Index('flows_idx', flows.columns.get('A'))
my_index.create(bind=engine)

# lets confirm it is there
inspector = reflection.Inspector.from_engine(engine)
print(inspector.get_indexes('flows'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多