Python 从sqlalchemy数据库查询数据

Python 从sqlalchemy数据库查询数据,python,python-2.7,sqlalchemy,Python,Python 2.7,Sqlalchemy,我对python和sql炼金术之类的东西非常陌生。上面的代码是网络控制器的相关部分。每当新数据包进来时,就会调用上面的代码。我的问题是 engine = create_engine('sqlite:///nwtopology.db', echo=False) Base = declarative_base() class SourcetoPort(Base): """""" __tablename__ = 'source_to_port' id = Column(Int

我对python和sql炼金术之类的东西非常陌生。上面的代码是网络控制器的相关部分。每当新数据包进来时,就会调用上面的代码。我的问题是

engine = create_engine('sqlite:///nwtopology.db', echo=False)
Base = declarative_base()

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String)

    #----------------------------------------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no




Session = sessionmaker(bind=engine)
session = Session()
self.mac_to_port[packet.src]=packet_in.in_port
if(self.matrix.get((packet.src,packet.dst))==None):
       self.matrix[(packet.src,packet.dst)]=0
       print "found a new flow"
       #create an databse entry with address and port
       entry = SourcetoPort(src_address=str(packet.src) , port_no=packet_in.in_port)
       #add the record to the session object
       session.add(entry)
       #add the record to the session object
       session.commit()
self.matrix[(packet.src,packet.dst)]+=1
print "incrementing flow count"
#if self.mac_to_port.get(packet.dst)!=None:
if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
#do stuff if the flow information is already in the databaase.
这是知道src_地址是否已经在数据库中的正确/最有效的方法吗?有人能建议一些更好的方法吗?依赖正计数似乎不是很严格。

一些建议

1) 确保您的表在src_地址字段上创建了索引。如果您正在使用SQLAlchemy创建模式索引,则可以通过对表定义的简单更改添加索引。(更多信息请参见)

2) 要检查是否有src_address=str(packet.dst)的记录,还有另一种方法可以使用EXISTS。因此,它不必扫描所有具有此类src_地址的记录,而是在找到具有此类字段值的第一条记录后立即返回结果

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String, index=True)
用exists查询替换count查询

if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
#do stuff if the flow information is already in the databaase.
3) 我不确定通过编写此脚本,您要解决的任务是什么。我只希望您不会在每次新的网络数据包到达网络接口时都启动新的python脚本。启动新的python解释器过程需要几秒钟,每秒可能有数千个数据包到达。 在后一种情况下,我会考虑运行这样的网络控制器程序作为守护进程,并在内存中缓存所有的信息。我还将实现数据库操作,以便在单独的线程甚至线程池中异步运行,这样它们就不会阻止控制网络流的主线程。

1) 确保您的表在src_地址字段上创建了索引。如果您正在使用SQLAlchemy创建模式索引,则可以通过对表定义的简单更改添加索引。(更多信息请参见)

2) 要检查是否有src_address=str(packet.dst)的记录,还有另一种方法可以使用EXISTS。因此,它不必扫描所有具有此类src_地址的记录,而是在找到具有此类字段值的第一条记录后立即返回结果

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String, index=True)
用exists查询替换count查询

if session.query(SourcetoPort).filter_by(src_address=str(packet.dst)).count():
#do stuff if the flow information is already in the databaase.
3) 我不确定通过编写此脚本,您要解决的任务是什么。我只希望您不会在每次新的网络数据包到达网络接口时都启动新的python脚本。启动新的python解释器过程需要几秒钟,每秒可能有数千个数据包到达。 在后一种情况下,我会考虑运行这样的网络控制器程序作为守护进程,并在内存中缓存所有的信息。我还将实现数据库操作,以便在单独的线程甚至线程池中异步运行,这样它们就不会阻塞控制网络流的主线程