小编say*_*yap的帖子

python从具有相同方法名称的不同路径的多重继承

下面的代码示例,可以super使用,或者C有来电A.fooB.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)

python multiple-inheritance

23
推荐指数
5
解决办法
2万
查看次数

pyodbc - 非常慢的批量插入速度

有了这个表:

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是:

  • 准备声明
  • 绑定所有参数
  • 执行

sql-server bulkinsert pyodbc

21
推荐指数
2
解决办法
1万
查看次数

将PostgreSQL string_agg移植到SQL Server,ORDER BY出现问题

在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 postgresql aggregate sql-order-by

5
推荐指数
1
解决办法
2579
查看次数

让SQL Server使用索引查找+键查找而不是聚集索引扫描,而不使用WITH(FORCESEEK)

版本: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 indexing query-hints

5
推荐指数
1
解决办法
6331
查看次数