我有两个简单的查询,它们在中小型表上实际上是相同的,这给了我完全不同的结果,我试图理解为什么。
这是最快的(1秒):
select *
from financials.income f
where f.itemtype in ('SALES') and f.company_id=6445
Run Code Online (Sandbox Code Playgroud)
这是慢速的(1 分钟):
create temp table t_co as select 6445 as company_id;
select f.*
from financials.income f
inner join t_co on f.company_id=t_co.company_id
where f.itemtype in ('SALES')
Run Code Online (Sandbox Code Playgroud)
以下是快速分析的解释:
[
{
"Execution Time": 97.271,
"Planning Time": 2.833,
"Plan": {
"Exact Heap Blocks": 29,
"Node Type": "Bitmap Heap Scan",
"Actual Total Time": 96.876,
"Shared Hit Blocks": 14822,
"Plans": [
{
"Node Type": "Bitmap Index Scan",
"Actual Total Time": 76.988,
"Shared Hit …Run Code Online (Sandbox Code Playgroud) 如果有人能指出我正确的方向,我会很感兴趣。
我有一个很长的存储过程(其中还包含对其中其他存储过程的调用),用于更新各种表。
如果我在管理工作室中运行存储过程,它运行良好。如果我从 pyodbc 调用它,那么:
我一直运行 pyodbc 来执行存储过程,并且没有任何问题 - 我知道我的连接或调用没有任何问题,就好像我将较短的存储过程替换到 python 代码中它工作正常的位置一样。
存储过程确实生成了一些“警告:空值被聚合或其他 SET 操作消除”消息,我认为这些可能会导致问题,但每当我尝试 SET ANSI_WARNINGS { ON | OFF } 无论是在存储过程内部还是在存储过程外部,我得到了 pyodbc.ProgrammingError
对问题有任何猜测吗?
Python 3.4 (have the same problem in 2.7), MSSQL, Windows 7
Run Code Online (Sandbox Code Playgroud)
更新:
import pyodbc as p
def getconn():
server='insertsqlservername'
dbase='insertdbasename'
connStr=('Driver={SQL Server};SERVER=' +
server + ';DATABASE=' + dbase + ';' +
'trusted=1')
conn = p.connect(connStr)
return conn
def runSQL():
conn=getconn()
cursor=conn.cursor()
try:
cursor.execute('exec InsertStoredProcName')
conn.commit()
except:
print('sys.exc_info()[0])
cursor.close()
conn.close()
Run Code Online (Sandbox Code Playgroud)
为了 100% 解决问题,似乎有两个组成部分: …