Search code examples
c++mysqlembedded-database

The mysql_query() is slow


I'm want to accelerate an encryption system recently. And in this system, it will use mysql, so it include the <mysql.h> file.

And I found the system runs slow not because of the encryption/decryption, but because of dealing with some sql statement.

It will use a memory database during the runing time, and will issue some query to the database by using the mysql_query() from <mysql.h>. The query is very simple, like:

UPDATE embedded_db.generic_prefix_staleness  SET stale = FALSE  WHERE cache_id = 2165265089

Like the query above, it will spend about 28.42 ms, it's too slow and I can't find the reason. So how can I fix it? Should I add/modify some config or do something else?

And the temporary database info:

void
Connect::do_connect(const std::string &server, const std::string &user,
                const std::string &passwd, uint port)
{
    const char *dummy_argv[] = {
        "progname",
        "--skip-grant-tables",
        "--skip-innodb",
        "--default-storage-engine=MEMORY",
        "--character-set-server=utf8",
        "--language=" MYSQL_BUILD_DIR "/sql/share/"
    };
    assert(0 == mysql_library_init(sizeof(dummy_argv)/sizeof(*dummy_argv),
                               const_cast<char**>(dummy_argv), 0));

    conn = mysql_init(NULL);

    /* Connect via TCP, and not via Unix domain sockets */
    const uint proto = MYSQL_PROTOCOL_TCP;
    mysql_options(conn, MYSQL_OPT_PROTOCOL, &proto);

    /* Connect to real server even if linked against embedded libmysqld */
    mysql_options(conn, MYSQL_OPT_USE_REMOTE_CONNECTION, 0);

    {
        my_bool reconnect = 1;
        /* automatically reconnect */
        mysql_options(conn, MYSQL_OPT_RECONNECT, &reconnect);
    }

    /* Connect to database */
    if (!mysql_real_connect(conn, server.c_str(), user.c_str(),
                        passwd.c_str(), 0, port, 0,
                        CLIENT_MULTI_STATEMENTS)) {
        LOG(warn) << "connecting to server " << server
              << " user " << user
              << " pwd " << passwd
              << " port " << port;
        LOG(warn) << "mysql_real_connect: " << mysql_error(conn);
        throw std::runtime_error("cannot connect");
    }
}

Update 1:

I have noticed that it use the function mysql_library_init(), and in it's makefile, it use -L$(MYBUILD)/libmysqld, so it use the Embedded MySQL Server. Maybe should I edit some config for the Embedded MySQL Server?


Solution

  • It use the embedded server, so we need add some config in the my.cnf like:

    [embedded]
    innodb_flush_log_at_trx_commit = 0