为什么我在 Python 中使用 QSqlQuery 时遇到这样的性能问题

Why i have such performance issues using QSqlQuery in Python

我必须将多个条目逐一添加到 sqlite 数据库中,因为我必须使用密钥。我正在用 QSqlQuery 做这件事并面临性能问题。以下代码需要 15 分钟才能完成 运行。

def import_vfc(self):
    fname = QtWidgets.QFileDialog.getOpenFileName(QtWidgets.QWidget(), 'Open a file', '',
                                                  'VCF Files (*.vcf)')
    try:
        contacts = import_contacts(fname[0])
        query = QSqlQuery()
        temp_phone_list = []
        temp_email_list = []

        for contact in contacts:
            query.exec("INSERT INTO person (forename, lastname, mergedAddress)" 
                       f"VALUES ('{contact.FirstName}', '{contact.LastName}', '{contact.Address}' )")
            
            query.exec("SELECT max(PersonId) FROM person")
            query.first()
            contact_id = query.value(0)
            
            for number in contact.Phonenumbers:
                temp_phone_list.append(number)
        
            phone_list = list(set(temp_phone_list))#remove duplicates
            for number in phone_list:
                query.exec("INSERT INTO phonenumbers (phonenumber)" f"VALUES ('{number}')")
                query.exec("SELECT max(PhoneNumberId) FROM Phonenumbers")
                query.first()
                phone_id = query.value(0)
                query.exec("INSERT INTO person_phone_junction (personid, phonenumberid) " f"VALUES ('{contact_id}', '{phone_id}')")

            for email in contact.Email:
                temp_email_list.append(email)

            email_list = list(set(temp_email_list))#remove duplicates
            for email in email_list:
                query.exec("INSERT INTO Emails (email)" f"VALUES ('{email}')")
                query.exec("SELECT max(EmailId) FROM Emails")
                query.first()
                email_id = query.value(0)
                query.exec("INSERT INTO person_email_junction (personid, emailid) " f"VALUES ('{contact_id}', '{email_id}')")
            
            if contact.Birthday:
                birthday = contact.Birthday
                query.exec(f"Update Person set Birthday= '{birthday}' where PersonId= {contact_id} ")
        
        self.setup_model()
        self.setup_phone_model()
        self.setup_email_model()
        self.tableView.selectRow(0)
    except Exception as e:
        ErrorLogger.WriteError(traceback.format_exc())
        QtWidgets.QMessageBox.critical(None, 'Exception raised', format(e))
        return        

第一部分,获取 import_contacts(fname[0]) 的联系人非常快。只有下面的qsql查询很慢。总而言之,有 168 个触点要插入,这不算多,而且应该快得多。我怎样才能提高性能? QSql是主要原因吗?

我找到了解决办法。循环中的列表越来越长:

                for number in contact.Phonenumbers:
                temp_phone_list.append(number)
        
            phone_list = list(set(temp_phone_list))#remove duplicates
            for number in phone_list:
                query.exec("INSERT INTO phonenumbers (phonenumber)" f"VALUES ('{number}')")
                query.exec("SELECT max(PhoneNumberId) FROM Phonenumbers")
                query.first()
                phone_id = query.value(0)
                query.exec("INSERT INTO person_phone_junction (personid, phonenumberid) " f"VALUES ('{contact_id}', '{phone_id}')")

所以简单地清除它们就完成了工作: temp_phone_list.clear() phone_list.clear()