为什么mysql_stmt_prepare(...)函数很慢?

时间:2015-06-24 02:09:13

标签: prepared-statement mariadb

我在我的项目中使用mariadb 10.0.19(windows版本)。 我使用mariadb C客户端库。

mysql_stmt_prepare函数很慢......为什么?

我感谢任何帮助。谢谢。

[Test Table]
CREATE TABLE `test_table` (
 `InfoSN` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
 `Col1` VARCHAR(50) NULL DEFAULT '0',
 `Col2` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col3` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col4` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col5` VARCHAR(50) NULL DEFAULT '0',
 `Col6` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col7` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col8` INT(10) UNSIGNED NULL DEFAULT '0',
 `Col9` INT(10) UNSIGNED NULL DEFAULT '0',
 PRIMARY KEY (`InfoSN`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=11001;

[Test Code]
#include "stdafx.h"

#include <mysql.h>
#include <iostream>


using namespace std;

#define DF_CONNECT_TIME_OUT 3


    int _tmain(int argc, _TCHAR* argv[])
    {
        MYSQL*      dbconn = NULL;
        MYSQL_STMT* stmt   = NULL;
        MYSQL_RES*  resultSet = NULL;
        MYSQL_ROW   record;

        int         port = 3306;
        int         timeout = DF_CONNECT_TIME_OUT;
        int         columnNo = 0;

        char        szQuery[256] = { 0, };
        char        szPreparedQuery[256] = { 0, };

        if (argc < 5)
        {
            cout << "DBPerformanceTest.exe [Host] [User] [Password] [DBName]" << endl;
            return -1;
        }

        dbconn = mysql_init(NULL);

        if (dbconn == NULL)
        {
            cout << "Error mysql_init()" << endl;
            return -1;
        }

        dbconn->reconnect = true;
        mysql_options(dbconn, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&timeout);

        if (!mysql_real_connect(dbconn, (char*)argv[1], (char*)argv[2], (char*)argv[3], (char*)argv[4], port, (char*)NULL, CLIENT_MULTI_STATEMENTS))
        {
            cout << "Error mysql_real_connect() ErrorNo : " << mysql_errno(dbconn) << ", ErrorMsg : " << mysql_error(dbconn) << endl;
            return -1;
        }

        stmt = mysql_stmt_init(dbconn);

        if (!stmt)
        {
            cout << "Error mysql_stmt_init() Out of memory. ErrorNo : " << mysql_stmt_errno(stmt) << ", ErrorMsg : " << mysql_stmt_error(stmt) << endl;
            return -1;
        }

        DWORD   dwStart = timeGetTime();

        for (int i = 100; i < 200; ++i)
        {
            sprintf(szQuery, "SELECT * FROM test_table WHERE InfoSN = %u", i);

            if (mysql_real_query(dbconn, szQuery, (unsigned long)strlen(szQuery)) == 0)
            {
                columnNo = 0;
                resultSet = mysql_store_result(dbconn);
            }
            else
            {
                cout << "Query Error : " << szQuery << ", ErrorNo : " << mysql_errno(dbconn) << ", ErrorMsg : " << mysql_error(dbconn) << endl;
                return -1;
            }

            record = mysql_fetch_row(resultSet);

            // Get Data & Process

            mysql_free_result(resultSet);
        }

        DWORD   dwEnd = timeGetTime();

        cout << "Normal Query Elapsed Time : " << dwEnd - dwStart << endl;

        DWORD   dwInfoSN = 0, dwCol2 = 0, dwCol3 = 0, dwCol4 = 0, dwCol6 = 0, dwCol7 = 0, dwCol8 = 0, dwCol9 = 0;
        char    szCol1[64] = { 0, };
        char    szCol5[64] = { 0, };
        DWORD   dwColLen[2] = { 0, };

        MYSQL_BIND  sp_param;
        MYSQL_BIND  sp_result[10];

        memset(&sp_param, 0, sizeof(MYSQL_BIND));
        memset(sp_result, 0, sizeof(MYSQL_BIND) * 10);

        dwStart = timeGetTime();

        for (int i = 200; i < 300; ++i)
        {
            sprintf(szPreparedQuery, "SELECT * FROM test_table WHERE InfoSN = ?");

            if (mysql_stmt_prepare(stmt, szPreparedQuery, strlen(szPreparedQuery)))
            {
                cout << "Error mysql_stmt_prepare() No : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
                return -1;
            }

            sp_param.buffer_type = MYSQL_TYPE_LONG;
            sp_param.buffer = (char *)&i;
            sp_param.buffer_length = sizeof(int);
            sp_param.length = 0;
            sp_param.is_null = 0;

            if (mysql_stmt_bind_param(stmt, &sp_param))
            {
                cout << "Error mysql_stmt_bind_param() ErrNo : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
                return -1;
            }

            if (mysql_stmt_execute(stmt))
            {
                cout << "Error mysql_stmt_execute() ErrNo : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
                return -1;
            }

            sp_result[0].buffer_type = MYSQL_TYPE_LONG;
            sp_result[0].buffer = (char *)&dwInfoSN;
            sp_result[0].buffer_length = sizeof(DWORD);
            sp_result[0].is_unsigned = 1;

            sp_result[1].buffer_type = MYSQL_TYPE_VAR_STRING;
            sp_result[1].buffer = (char *)szCol1;
            sp_result[1].buffer_length = 64;
            sp_result[1].length = &dwColLen[0];

            sp_result[2].buffer_type = MYSQL_TYPE_LONG;
            sp_result[2].buffer = (char *)&dwCol2;
            sp_result[2].buffer_length = sizeof(DWORD);
            sp_result[2].is_unsigned = 1;

            sp_result[3].buffer_type = MYSQL_TYPE_LONG;
            sp_result[3].buffer = (char *)&dwCol3;
            sp_result[3].buffer_length = sizeof(DWORD);
            sp_result[3].is_unsigned = 1;

            sp_result[4].buffer_type = MYSQL_TYPE_LONG;
            sp_result[4].buffer = (char *)&dwCol4;
            sp_result[4].buffer_length = sizeof(DWORD);
            sp_result[4].is_unsigned = 1;

            sp_result[5].buffer_type = MYSQL_TYPE_VAR_STRING;
            sp_result[5].buffer = (char *)szCol5;
            sp_result[5].buffer_length = 64;
            sp_result[5].length = &dwColLen[1];

            sp_result[6].buffer_type = MYSQL_TYPE_LONG;
            sp_result[6].buffer = (char *)&dwCol6;
            sp_result[6].buffer_length = sizeof(DWORD);
            sp_result[6].is_unsigned = 1;

            sp_result[7].buffer_type = MYSQL_TYPE_LONG;
            sp_result[7].buffer = (char *)&dwCol7;
            sp_result[7].buffer_length = sizeof(DWORD);
            sp_result[7].is_unsigned = 1;

            sp_result[8].buffer_type = MYSQL_TYPE_LONG;
            sp_result[8].buffer = (char *)&dwCol8;
            sp_result[8].buffer_length = sizeof(DWORD);
            sp_result[8].is_unsigned = 1;

            sp_result[9].buffer_type = MYSQL_TYPE_LONG;
            sp_result[9].buffer = (char *)&dwCol9;
            sp_result[9].buffer_length = sizeof(DWORD);
            sp_result[9].is_unsigned = 1;

            if (mysql_stmt_bind_result(stmt, sp_result))
            {
                cout << "Error mysql_stmt_bind_result() ErrNo : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
                return -1;
            }

            if (mysql_stmt_store_result(stmt))
            {
                cout << "Error mysql_stmt_store_result() ErrNo : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
                return -1;
            }

            while (1)
            {
                int iState = mysql_stmt_fetch(stmt);

                if (iState == 0 || iState == MYSQL_NO_DATA)
                    break;
            }

            mysql_stmt_free_result(stmt);
        }

        dwEnd = timeGetTime();

        cout << "Prepared Query Elapsed Time : " << dwEnd - dwStart << endl;

        mysql_close(dbconn);
        mysql_stmt_close(stmt);

        return 0;
    }

[结果] 正常查询已用时间:39 准备好的查询已用时间:5508

[测试2]

...
sprintf(szPreparedQuery, "SELECT * FROM test_table WHERE InfoSN = ?");

if (mysql_stmt_prepare(stmt, szPreparedQuery, strlen(szPreparedQuery)))
{
    cout << "Error mysql_stmt_prepare() No : " << mysql_stmt_errno(stmt) << ", Msg : " << mysql_stmt_error(stmt) << endl;
    return -1;
}

for (int i = 200; i < 300; ++i)
{
Bind & Execute...
...
}

[结果2] 正常查询已用时间:47 准备好的查询已用时间:25

0 个答案:

没有答案