在 SQLite 中,准备好的语句真的能提高性能吗?

发布于 2024-08-10 19:36:23 字数 2636 浏览 4 评论 0原文

我听说使用 SQLite 准备好的语句应该可以提高性能。我编写了一些代码来测试它,并且没有发现使用它们在性能上有任何差异。所以,我想我的代码可能不正确。如果您发现我的做法有任何错误,请告诉我...

[self testPrep:NO dbConn:dbConn];
[self testPrep:YES dbConn:dbConn];

reuse=0
recs=2000
2009-11-09 10:39:18 -0800
processing...
2009-11-09 10:39:32 -0800

reuse=1
recs=2000
2009-11-09 10:39:32 -0800
processing...
2009-11-09 10:39:46 -0800

-(void)testPrep:(BOOL)reuse dbConn:(sqlite3*)dbConn{
    int recs = 2000;
    NSString *sql;
    sqlite3_stmt *stmt;

    sql = @"DROP TABLE test";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    sql = @"CREATE TABLE test (id INT,field1 INT, field2 INT,field3 INT,field4 INT,field5 INT,field6 INT,field7 INT,field8 INT,field9 INT,field10 INT)";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    for(int i=0;i<recs;i++){
        sql = @"INSERT INTO test (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10) VALUES (%d,1,2,3,4,5,6,7,8,9,10)";
        sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
    }

    sql = @"BEGIN";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    if (reuse){
        sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
        sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
    }

    NSLog(@"reuse=%d",reuse);
    NSLog(@"recs=%d",recs);
    NSDate *before = [NSDate date];
    NSLog([before description]);
    NSLog(@"processing...");
    for(int i=0;i<recs;i++){
        if (!reuse){
            sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
            sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
        }
        sqlite3_bind_int(stmt, 1, 1);
        sqlite3_bind_int(stmt, 2, 2);
        sqlite3_bind_int(stmt, 3, 3);
        sqlite3_bind_int(stmt, 4, 4);
        sqlite3_bind_int(stmt, 5, 5);
        sqlite3_bind_int(stmt, 6, 6);
        sqlite3_bind_int(stmt, 7, 7);
        sqlite3_bind_int(stmt, 8, 8);
        sqlite3_bind_int(stmt, 9, 9);
        sqlite3_bind_int(stmt, 10, 10);
        sqlite3_bind_int(stmt, 11, i);

        while(sqlite3_step(stmt) == SQLITE_ROW) {
        }
        sqlite3_reset(stmt);
    }

    sql = @"BEGIN";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    NSDate *after = [NSDate date];
    NSLog([after description]);
}

I have heard that prepared statements with SQLite should improve performance. I wrote some code to test that, and did not see any difference in performance with using them. So, I thought maybe my code was incorrect. Please let me know if you see any errors in how I'm doing this...

[self testPrep:NO dbConn:dbConn];
[self testPrep:YES dbConn:dbConn];

reuse=0
recs=2000
2009-11-09 10:39:18 -0800
processing...
2009-11-09 10:39:32 -0800

reuse=1
recs=2000
2009-11-09 10:39:32 -0800
processing...
2009-11-09 10:39:46 -0800

-(void)testPrep:(BOOL)reuse dbConn:(sqlite3*)dbConn{
    int recs = 2000;
    NSString *sql;
    sqlite3_stmt *stmt;

    sql = @"DROP TABLE test";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    sql = @"CREATE TABLE test (id INT,field1 INT, field2 INT,field3 INT,field4 INT,field5 INT,field6 INT,field7 INT,field8 INT,field9 INT,field10 INT)";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    for(int i=0;i<recs;i++){
        sql = @"INSERT INTO test (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10) VALUES (%d,1,2,3,4,5,6,7,8,9,10)";
        sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
    }

    sql = @"BEGIN";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    if (reuse){
        sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
        sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
    }

    NSLog(@"reuse=%d",reuse);
    NSLog(@"recs=%d",recs);
    NSDate *before = [NSDate date];
    NSLog([before description]);
    NSLog(@"processing...");
    for(int i=0;i<recs;i++){
        if (!reuse){
            sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
            sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
        }
        sqlite3_bind_int(stmt, 1, 1);
        sqlite3_bind_int(stmt, 2, 2);
        sqlite3_bind_int(stmt, 3, 3);
        sqlite3_bind_int(stmt, 4, 4);
        sqlite3_bind_int(stmt, 5, 5);
        sqlite3_bind_int(stmt, 6, 6);
        sqlite3_bind_int(stmt, 7, 7);
        sqlite3_bind_int(stmt, 8, 8);
        sqlite3_bind_int(stmt, 9, 9);
        sqlite3_bind_int(stmt, 10, 10);
        sqlite3_bind_int(stmt, 11, i);

        while(sqlite3_step(stmt) == SQLITE_ROW) {
        }
        sqlite3_reset(stmt);
    }

    sql = @"BEGIN";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);

    NSDate *after = [NSDate date];
    NSLog([after description]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

且行且努力 2024-08-17 19:36:23

准备好的语句通过在 查询优化器已找到最佳计划。

如果您使用的查询没有复杂的计划(例如没有连接的简单选择/插入),那么准备好的语句不会给您带来很大的改进,因为优化器会快速找到最佳计划。

但是,如果您使用具有一些联接并使用一些索引的查询运行相同的测试,您将看到性能差异,因为优化器不会在每次查询时都运行。

Prepared statements improve performance by caching the execution plan for a query after the query optimizer has found the best plan.

If the query you're using doesn't have a complicated plan (such as simple selects/inserts with no joins), then prepared statements won't give you a big improvement since the optimizer will quickly find the best plan.

However, if you ran the same test with a query that had a few joins and used some indexes, you would see the performance difference since the optimizer wouldn't be run every time the query is.

陌路黄昏 2024-08-17 19:36:23

是的 - 无论您使用 sqlite3_exec() 还是使用 sqlite3_prepare_v2() / sqlite3_bind_xxx() / sqlite3_step() 进行批量插入,都会产生巨大的差异。

sqlite3_exec() 只是一个方便的方法。在内部,它只是调用相同的 sqlite3_prepare_v2() 和 sqlite3_step() 序列。您的示例代码在文字字符串上一遍又一遍地调用 sqlite3_exec()

for(int i=0;i<recs;i++){
    sql = @"INSERT INTO test (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10) VALUES (%d,1,2,3,4,5,6,7,8,9,10)";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
}

我不知道 SQLite 解析器的内部工作原理,但也许解析器足够聪明,可以识别出您是使用相同的文字字符串,然后在每次迭代时跳过重新解析/重新编译。

如果您尝试使用更改的值进行相同的实验 - 您会发现性能差异更大。

Yes - it makes a huge difference whether your using sqlite3_exec() vs. sqlite3_prepare_v2() / sqlite3_bind_xxx() / sqlite3_step() for bulk inserts.

sqlite3_exec() is only a convenience method. Internally it just calls the same sequence of sqlite3_prepare_v2() and sqlite3_step(). Your example code is calling sqlite3_exec() over-and-over on a literal string:

for(int i=0;i<recs;i++){
    sql = @"INSERT INTO test (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10) VALUES (%d,1,2,3,4,5,6,7,8,9,10)";
    sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
}

I don't know the inner workings of the SQLite parser, but perhaps the parser is smart enough to recognize that you are using the same literal string and then skips re-parsing/re-compiling with every iteration.

If you try the same experiment with values that change - you'll see a much bigger difference in performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文