脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python查询oracle数据库速度慢的解决方案

Python查询oracle数据库速度慢的解决方案

2021-10-10 22:15qianya1013 Python

这篇文章主要介绍了Python查询oracle数据库速度慢的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
conn = cx_Oracle.connect('username/password@ip:port/servername')
cur = conn.cursor()
cur.execute('SELECT * FROM "db"."table"')

cur是一个迭代器,不要用fetchall一次性取完数据

直接 for row in cur 即可取数据

使用:sqlalchemy

?
1
2
3
4
5
6
7
8
9
10
11
MySQL-Python
    mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
 
pymysql
    mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
 
MySQL-Connector
    mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
 
cx_Oracle
    oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
?
1
2
3
4
5
6
7
8
9
10
11
create_engine('oracle+cx_oracle://{a}:{b}@{c}:{d}/?service_name={e}'.format(a,b,c,d,e))
create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s/%(database)s?charset=utf8' % laoshifu_info)
 
df = pd.read_sql_table(table_name="table_name", con=engine) 
(the function to_sql is case-sensitive,Found the root cause from DBMS (mysql) autoconvert the table name to lowercase.)
df = pd.read_sql_query(sql=sql,con=engine)  # 很慢
ordf = pd.read_sql("SELECT * FROM db.table ",engine,chunksize=50000)
dflist = []
for chunk in ordf:
    dflist.append(chunk)
df = pd.concat(dflist)

补充:Python3 Cx_oracle 的一些使用技巧

Cx_oracle的一些使用技巧

工作中的数据库采用oracle。访问oracle数据库一般都采用cx_oracle包来完成,API很清晰,操作效率也比较高,而且oracle官方好像对cx_oracle也非常支持,提供了丰富的文档。这里讨论一些使用技巧,作为记录,可能对你也有用。

我最近用python写了一个小工具,这个工具根据客户端的请求查询数据库,并将结果集以json的方式返回。请求的格式如下:

?
1
2
3
4
5
6
7
8
{
    fields : [
        {name : "project_id", type : "string"},
        {name : "project_name", type : "string"}
    ],
    
    sql : "select t.project_id, t.project_name from dp_project t"
}

即,客户端描述自己想要的元数据信息(字段名称,字段类型),以及SQL语句,服务器端根据此信息查询数据库,并将返回组织成客户端在fields中描述的那样。

cx_oracle默认从cursor中fetch出来的数据是一个元组,按照SQL中的顺序组织,但是我希望返回的是一个字典结构,这个可以通过设置cursor的rowfactory属性来实现,定义一个rowfactory的回调函数:

?
1
2
3
4
5
def makedict(self, cursor):
    cols = [d[0] for d in cursor.description]    
    def createrow(*args):
        return dict(zip(cols, args))    
    return createrow

这个函数返回一个函数:createrow。可能有点绕口,仔细想想就清晰了。cursor中带有足够的信息来生成这个字典,如cursor的description的值为:

?
1
2
3
4
[
('PROJECT_ID', <;type 'cx_Oracle.STRING'>, 40, 40, 0, 0, 0),
('PROJECT_NAME', <;type 'cx_Oracle.STRING'>, 50, 50, 0, 0, 1)
]

我们需要的是cursor.description的第一列,zip函数将cols和默认的那个元组合成为一个新的元组,再用dict转换为一个新的字典对象返回。

然后将这个返回函数的函数注册给cursor的rowfactory即可:

?
1
cursor.rowfactory = self.makedict(cursor)

这样,我们使用cursor.fetchall/fetchone的时候,取出来的就成为一个字典对象,很方便将其序列化为json格式返回。

另一个技巧是关于将查询到的结果中,字符串类型的字段转换为unicode,数值类型的不做处理:

?
1
2
3
def outtypehandler(self, cursor, name, dtype, size, p, s):
    if dtype in (oracle.STRING, oracle.FIXED_CHAR):
        return cursor.var(unicode, size, cursor.arraysize)

将connection对象的outputtypehandler注册为此函数即可:

?
1
2
connection = oracle.connect(self.constr)
connection.outputtypehandler = self.outtypehandler

通用查询的这个小工具还在开发中,等完成了再整理一下。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/Outsider_Lily/article/details/85835016

延伸 · 阅读

精彩推荐