C++ 中的 MySQL慢慢地跑

发布于 2024-12-03 12:00:00 字数 2227 浏览 1 评论 0原文

我们有MySQL数据库。我们正在尝试使用 C++ 代码中的 ODBC 驱动程序连接到此 MySQL 数据库。我们使用的ODBC驱动程序版本为3.51。

数据库结构如下: 我们在数据库 D1 中有一个会话表 CONV_X1,其中包含客户端和服务器计算机的 IP 地址以及与此会话对应的数据,格式如下:

ProtocolID ServerID ClientID TimeStamp InOctets OutOctets

     24  167772161  167772162  1310121900         0  1.741e+006 
    632  167772161  167772162  1310121900         0  1.741e+006 
     24  167772161  167772162  1310122800         0    1.8e+006 

我们在另一个数据库 D2 中有另一个表 PROT_NAME,其中包含信息对于此协议 ID。我们将数据库 D1 中的 CONV_X1 表中的 ProtocolID 与数据库 D2 中的 PROT_NAME 表进行映射,以获取协议名称。

从 information_schema 中,表名 CONV_X1 被采用,因为 D1 中可以有多个具有不同 ID 的同名表(例如 CONV_X1、CONV_X2 等)。

我们正在以这种格式从该数据库中提取数据

ServerID:ClientID_ProtocolName_InOctets_tableName
ClientID:ServerID_ProtocolName_InOctets_tableName 

,对于 1310121900 时间戳的数据,我们正在操作的格式如下

10.0.0.1:10.0.0.2_ftp_InOctets_CONV_X1 data is 0
10.0.0.2:10.0.0.1_ftp_InOctets_CONV_X1 data is 1.741e+006

10.0.0.1:10.0.0.2_udp_InOctets_CONV_X1 data is 0
10.0.0.2:10.0.0.1_udp_InOctets_CONV_X1 data is 1.741e+006

我们正在使用以下查询来获得结果

select distinct concat(inet_ntoa(np.serverid), ':',inet_ntoa(np.clientid),'_inoctets_',rp.name,'_',c.table_name)HostCounter1, concat(inet_ntoa(np.clientid),':',inet_ntoa(np.serverid),'_inoctets_',rp.name,'_',c.table_name)HostCounter2, np.inoctets value1 ,np.outoctets value2, from_unixtime(timestamp) from information_schema.`COLUMNS` c,D1.CONV_X1 np,D2.PROT_NAME rp where c.table_schema='D1' AND c.table_name ='CONV_X1' AND np.protocolid=rp.id AND np.timestamp between unix_timestamp('2011-08-31 10:33:14') AND unix_timestamp('2011-08-31 11:33:14') order by timestamp;

这将以以下格式给出输出:

HostCounter1 HostCounter2 value1 value2 from_unixtime(timestamp)
10.0.0.1:10.0.0.2_ftp_InOctets_CONV_X1 10.0.0.2:10.0.0.1_ftp_InOctets_CONV_X1 0 1.741e+006 1310121900(in unix format)
10.0.0.1:10.0.0.2_udp_InOctets_CONV_X1 10.0.0.2:10.0.0.1_udp_InOctets_CONV_X1 0 1.741e+006

我们的 C++ 代码使用 SQL 调用执行此操作SQLDriverConnect()、SQLExecDirect(),这些调用需要花费大量时间来连接到数据库并执行此查询。因此,我们的 C++ 代码面临性能问题。 请告诉我们是否有任何方法可以解决性能问题。 ODBC驱动程序的改变对我们有帮助吗? 我们可以优化查询吗?如果是这样怎么办?

We have MySQL database. We are trying to connect to this MySQL database with ODBC drivers in C++ code. The ODBC drivers used by us is with version 3.51.

The database structure is as follows:
We have a conversation table CONV_X1 in database D1, that contains the IP address of client and server machines and the data corresponding to this conversation in the following format:

ProtocolID ServerID ClientID TimeStamp InOctets OutOctets

     24  167772161  167772162  1310121900         0  1.741e+006 
    632  167772161  167772162  1310121900         0  1.741e+006 
     24  167772161  167772162  1310122800         0    1.8e+006 

We have another table PROT_NAME in another database D2 that has information for this protocol ID. We are mapping ProtocolID from CONV_X1 table in database D1 with PROT_NAME table in database D2 to get the protocol name.

From information_schema the table name CONV_X1 is taken as there can be multiple tables with the same name in D1 with different ID(example. CONV_X1, CONV_X2 etc.)

We are extracting data from this database in the format

ServerID:ClientID_ProtocolName_InOctets_tableName
ClientID:ServerID_ProtocolName_InOctets_tableName 

so, for 1310121900 timestamp the data we are manipulating is in following format

10.0.0.1:10.0.0.2_ftp_InOctets_CONV_X1 data is 0
10.0.0.2:10.0.0.1_ftp_InOctets_CONV_X1 data is 1.741e+006

10.0.0.1:10.0.0.2_udp_InOctets_CONV_X1 data is 0
10.0.0.2:10.0.0.1_udp_InOctets_CONV_X1 data is 1.741e+006

We are using the following query to achieve the result

select distinct concat(inet_ntoa(np.serverid), ':',inet_ntoa(np.clientid),'_inoctets_',rp.name,'_',c.table_name)HostCounter1, concat(inet_ntoa(np.clientid),':',inet_ntoa(np.serverid),'_inoctets_',rp.name,'_',c.table_name)HostCounter2, np.inoctets value1 ,np.outoctets value2, from_unixtime(timestamp) from information_schema.`COLUMNS` c,D1.CONV_X1 np,D2.PROT_NAME rp where c.table_schema='D1' AND c.table_name ='CONV_X1' AND np.protocolid=rp.id AND np.timestamp between unix_timestamp('2011-08-31 10:33:14') AND unix_timestamp('2011-08-31 11:33:14') order by timestamp;

This will give output in following format:

HostCounter1 HostCounter2 value1 value2 from_unixtime(timestamp)
10.0.0.1:10.0.0.2_ftp_InOctets_CONV_X1 10.0.0.2:10.0.0.1_ftp_InOctets_CONV_X1 0 1.741e+006 1310121900(in unix format)
10.0.0.1:10.0.0.2_udp_InOctets_CONV_X1 10.0.0.2:10.0.0.1_udp_InOctets_CONV_X1 0 1.741e+006

Our C++ code executes this using SQL calls SQLDriverConnect(), SQLExecDirect(), these calls are taking a lot of time to connect to database and execute this query. Due to this we are facing performance issue with our C++ code.
Please let us know is there any way to resolve the performance issue.
Will the change in ODBC driver can help us?
Can we optimize the query? If so how?

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

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

发布评论

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

评论(1

奶茶白久 2024-12-10 12:00:00

尝试查看查询计划并确保您有适当的索引。

Try looking at the query plan and ensure that you have the appropriate indexes.

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