Python查詢oracle數(shù)據(jù)庫(kù)速度慢的解決方案
如下所示:
conn = cx_Oracle.connect(’username/password@ip:port/servername’)cur = conn.cursor()cur.execute(’SELECT * FROM 'db'.'table'’)
cur是一個(gè)迭代器,不要用fetchall一次性取完數(shù)據(jù)
直接 for row in cur 即可取數(shù)據(jù)
使用:sqlalchemyMySQL-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}:{b}@{c}:is2tiay/?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)
補(bǔ)充:Python3 Cx_oracle 的一些使用技巧
Cx_oracle的一些使用技巧工作中的數(shù)據(jù)庫(kù)采用oracle。訪問(wèn)oracle數(shù)據(jù)庫(kù)一般都采用cx_oracle包來(lái)完成,API很清晰,操作效率也比較高,而且oracle官方好像對(duì)cx_oracle也非常支持,提供了豐富的文檔。這里討論一些使用技巧,作為記錄,可能對(duì)你也有用。
我最近用python寫了一個(gè)小工具,這個(gè)工具根據(jù)客戶端的請(qǐng)求查詢數(shù)據(jù)庫(kù),并將結(jié)果集以json的方式返回。請(qǐng)求的格式如下:
{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語(yǔ)句,服務(wù)器端根據(jù)此信息查詢數(shù)據(jù)庫(kù),并將返回組織成客戶端在fields中描述的那樣。
cx_oracle默認(rèn)從cursor中fetch出來(lái)的數(shù)據(jù)是一個(gè)元組,按照SQL中的順序組織,但是我希望返回的是一個(gè)字典結(jié)構(gòu),這個(gè)可以通過(guò)設(shè)置cursor的rowfactory屬性來(lái)實(shí)現(xiàn),定義一個(gè)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
這個(gè)函數(shù)返回一個(gè)函數(shù):createrow。可能有點(diǎn)繞口,仔細(xì)想想就清晰了。cursor中帶有足夠的信息來(lái)生成這個(gè)字典,如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和默認(rèn)的那個(gè)元組合成為一個(gè)新的元組,再用dict轉(zhuǎn)換為一個(gè)新的字典對(duì)象返回。
然后將這個(gè)返回函數(shù)的函數(shù)注冊(cè)給cursor的rowfactory即可:
cursor.rowfactory = self.makedict(cursor)
這樣,我們使用cursor.fetchall/fetchone的時(shí)候,取出來(lái)的就成為一個(gè)字典對(duì)象,很方便將其序列化為json格式返回。
另一個(gè)技巧是關(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對(duì)象的outputtypehandler注冊(cè)為此函數(shù)即可:
connection = oracle.connect(self.constr) connection.outputtypehandler = self.outtypehandler
通用查詢的這個(gè)小工具還在開(kāi)發(fā)中,等完成了再整理一下。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁(yè)一樣漂亮2. 利用CSS制作3D動(dòng)畫3. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效4. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法5. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼6. HTML5 Canvas繪制圖形從入門到精通7. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題8. html5手機(jī)觸屏touch事件介紹9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)
