成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Python查詢oracle數(shù)據(jù)庫速度慢的解決方案

瀏覽:63日期:2022-06-22 13:51:08

如下所示:

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

cur是一個迭代器,不要用fetchall一次性取完數(shù)據(jù)

直接 for row in cur 即可取數(shù)據(jù)

使用:sqlalchemy

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...]

create_engine(’oracle+cx_oracle://{a}:@{c}:qcy8sy8k88/?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,F(xiàn)ound 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的一些使用技巧

工作中的數(shù)據(jù)庫采用oracle。訪問oracle數(shù)據(jù)庫一般都采用cx_oracle包來完成,API很清晰,操作效率也比較高,而且oracle官方好像對cx_oracle也非常支持,提供了豐富的文檔。這里討論一些使用技巧,作為記錄,可能對你也有用。

我最近用python寫了一個小工具,這個工具根據(jù)客戶端的請求查詢數(shù)據(jù)庫,并將結(jié)果集以json的方式返回。請求的格式如下:

{fields : [ {name : 'project_id', type : 'string'}, {name : 'project_name', type : 'string'}],sql : 'select t.project_id, t.project_name from dp_project t' }

即,客戶端描述自己想要的元數(shù)據(jù)信息(字段名稱,字段類型),以及SQL語句,服務器端根據(jù)此信息查詢數(shù)據(jù)庫,并將返回組織成客戶端在fields中描述的那樣。

cx_oracle默認從cursor中fetch出來的數(shù)據(jù)是一個元組,按照SQL中的順序組織,但是我希望返回的是一個字典結(jié)構(gòu),這個可以通過設置cursor的rowfactory屬性來實現(xiàn),定義一個rowfactory的回調(diào)函數(shù):

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

這個函數(shù)返回一個函數(shù):createrow。可能有點繞口,仔細想想就清晰了。cursor中帶有足夠的信息來生成這個字典,如cursor的description的值為:

[ (’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函數(shù)將cols和默認的那個元組合成為一個新的元組,再用dict轉(zhuǎn)換為一個新的字典對象返回。

然后將這個返回函數(shù)的函數(shù)注冊給cursor的rowfactory即可:

cursor.rowfactory = self.makedict(cursor)

這樣,我們使用cursor.fetchall/fetchone的時候,取出來的就成為一個字典對象,很方便將其序列化為json格式返回。

另一個技巧是關(guān)于將查詢到的結(jié)果中,字符串類型的字段轉(zhuǎn)換為unicode,數(shù)值類型的不做處理:

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注冊為此函數(shù)即可:

connection = oracle.connect(self.constr) connection.outputtypehandler = self.outtypehandler

通用查詢的這個小工具還在開發(fā)中,等完成了再整理一下。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關(guān)文章: