SQLite3 sqlite3_step函数的性能问题

时间:2011-04-15 14:53:54

标签: c++ sqlite

使用sqlite3_step函数时,我遇到了特定查询的问题:

SELECT DISTINCT interfaces_int_id,device_dev_id FROM devInterface 
INNER JOIN interfaces ON devInterface.interfaces_int_id=interfaces.intf_id 
INNER JOIN nlink ON nlink.interfaces_intf_id=interfaces.intf_id 
INNER JOIN neighbor ON neighbor.neighbor_id=nlink.neighbor_neighbor_id 

我只发布了相关的查询部分。

对于查询数据库,我使用this code

调试时,它会挂起

    while(true)
    {
--->    result = sqlite3_step(statement);

        if(result == SQLITE_ROW)
        {
            std::vector<std::string> values;
            ...
        }
        ...
    }

根据数据库大小,有时需要几分钟才能得到结果。但是当使用Firefox插件“SQLite Manager”时,只需1-2秒。

如果查询需要几分钟,“邻居”和“nlink”表的条目超过150k。

为了建立数据库,我使用了MySQL Workbench工具的正向工程师功能,并修改了sqlite3的语法:

CREATE TABLE IF NOT EXISTS device(dev_id INTEGER PRIMARY KEY AUTOINCREMENT, type INT, hwtype INT, dataSource INT, hostname TEXT, sw_version TEXT, stpBridgeID TEXT, stpProtocol TEXT);

CREATE TABLE IF NOT EXISTS interfaces(intf_id INTEGER PRIMARY KEY AUTOINCREMENT, intfName TEXT, intfType TEXT, phl INT, macAddress TEXT, ipAddress TEXT, subnetMask TEXT, duplex TEXT, speed TEXT, status TEXT, description TEXT, l2l3 TEXT, errLvl INT, loadLvl INT, channel_intf_id INT, vpc_id INT, CONSTRAINT fk_interfaces_interfaces1 FOREIGN KEY (channel_intf_id) REFERENCES interfaces (intf_id) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE IF NOT EXISTS devInterface (interfaces_int_id INT, device_dev_id INT, cdp_cdp_id INT, PRIMARY KEY (interfaces_int_id, device_dev_id, cdp_cdp_id), CONSTRAINT fk_dev_interface_interfaces1 FOREIGN KEY (interfaces_int_id) REFERENCES interfaces (intf_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_dev_interface_device1 FOREIGN KEY (device_dev_id) REFERENCES device (dev_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_devInterface_cdp1 FOREIGN KEY (cdp_cdp_id) REFERENCES cdp (cdp_id) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE IF NOT EXISTS neighbor (neighbor_id INTEGER PRIMARY KEY AUTOINCREMENT, l2_addr TEXT NULL , l3_addr TEXT NULL);

CREATE TABLE IF NOT EXISTS nlink (neighbor_neighbor_id INT, interfaces_intf_id INT, PRIMARY KEY (neighbor_neighbor_id, interfaces_intf_id), CONSTRAINT fk_table1_neighbor1  FOREIGN KEY (neighbor_neighbor_id ) REFERENCES neighbor (neighbor_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_table1_interfaces1 FOREIGN KEY (interfaces_intf_id) REFERENCES interfaces (intf_id) ON DELETE CASCADE ON UPDATE CASCADE);  

编辑:SQLITE版本: 3.6.22

1 个答案:

答案 0 :(得分:0)

将SQLite升级到最新版本(3.7.6.2)后,性能要好得多。