下面的代码示例,可以super
使用,或者C
有来电A.foo
和B.foo
明确?
class A(object):
def foo(self):
print 'A.foo()'
class B(object):
def foo(self):
print 'B.foo()'
class C(A, B):
def foo(self):
print 'C.foo()'
A.foo(self)
B.foo(self)
Run Code Online (Sandbox Code Playgroud) 有了这个表:
CREATE TABLE test_insert (
col1 INT,
col2 VARCHAR(10),
col3 DATE
)
Run Code Online (Sandbox Code Playgroud)
以下代码需要40秒才能运行:
import pyodbc
from datetime import date
conn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};'
'SERVER=localhost;DATABASE=test;UID=xxx;PWD=yyy')
rows = []
row = [1, 'abc', date.today()]
for i in range(10000):
rows.append(row)
cursor = conn.cursor()
cursor.executemany('INSERT INTO test_insert VALUES (?, ?, ?)', rows)
conn.commit()
Run Code Online (Sandbox Code Playgroud)
psycopg2的等效代码只需3秒.我不认为mssql比postgresql慢得多.有关如何在使用pyodbc时提高批量插入速度的任何想法?
编辑:在ghoerz发现之后添加一些注释
在pyodbc中,流程executemany
是:
在ceODBC,流程executemany
是:
在PostgreSQL中,有一个非常有用的string_agg函数,允许查询如下:
SELECT
contacts.first_name,
contacts.last_name,
(
SELECT
string_agg(number, ', ' ORDER BY phones.priority)
FROM
phones
WHERE
phones.contact_id = contacts.id
) AS phone_numbers
FROM
contacts
Run Code Online (Sandbox Code Playgroud)
同样,这可以使用group_concat在MySQL中完成.
现在,我试图将其作为CLR用户定义的聚合端口导入SQL Server.代码本身不是问题,因为在整个网络上有成千上万的例子来创建这个特定的聚合(这就是问题:为什么它不是SQL Server的一部分?).
问题是,我找不到一种干净的方式来应用ORDER BY,因为SQL Server不仅不支持聚合函数中的ORDER BY,它还禁止在子查询中使用ORDER BY.我最好的选择是:
SELECT
contacts.first_name,
contacts.last_name,
(
SELECT
dbo.string_agg(number, ', ')
FROM
(
SELECT TOP <some really large number>
number
FROM
phones
WHERE
phones.contact_id = contacts.id
ORDER BY
phones.priority
) AS phones
) AS phone_numbers
FROM
contacts
Run Code Online (Sandbox Code Playgroud)
有更好的解决方法吗?是的,我已经阅读了子查询中允许的order by子句,我敢说,这是子查询中ORDER BY的有效用例.
版本:SQL Server 2008 R2
数据库:来自http://msftdbprodsamples.codeplex.com/releases/view/55926的 AdventureWorks 2008R2
查询:
SELECT TOP 10
*
FROM
Person.Person --WITH (FORCESEEK)
WHERE
LastName like 'Max%'
OR EXISTS (
SELECT
1
FROM
Person.PersonPhone
WHERE
Person.PersonPhone.BusinessEntityID = Person.Person.BusinessEntityID
AND Person.PersonPhone.PhoneNumber LIKE '122%'
)
ORDER BY Person.Person.BusinessEntityID DESC
Run Code Online (Sandbox Code Playgroud)
没有任何查询提示,SQL Server将使用聚合索引扫描,这是IO密集型:
Table 'PersonPhone'. Scan count 14170, logical reads 28446, physical reads 15, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Person'. Scan count 1, logical reads 2844, physical reads 3, …
Run Code Online (Sandbox Code Playgroud) sql-server ×3
aggregate ×1
bulkinsert ×1
indexing ×1
postgresql ×1
pyodbc ×1
python ×1
query-hints ×1
sql-order-by ×1