首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

基於 MySQL 的高效能資料庫應用開發:選型、安裝、C API 實戰與效能實測

     編輯:MySQL 資料庫相關知識

從 MS SQL Server 遷移到 MySQL:高效能股市行情系統實戰紀錄

背景與選型

  • 需求:1000+ 支股票、每分鐘更新、大量並發查詢、PC Server + Windows NT、高性價比
  • 原架構:SQL Server 6.5 + ODBC + VC++ 6.0 → 效能不足
  • 改用 SQL Server DB-Library 繞過 ODBC → 30 updates/sec、20-30 並發
  • 參考網易 FreeBSD + MySQL 架構,評估 MySQL Benchmark:
操作MySQLMySQL_ODBCMS-SQL_ODBCOracle_ODBC
索引讀 200 萬行367s464s1,634s20,800s
插入 35 萬行381s619s4,012s11,291s

MySQL 原生 C API 效能碾壓同平台商業資料庫。

MySQL on Windows NT 安裝與初始化

  1. 下載 mysql-shareware-3.22.32-win.zip,執行 Setup
  2. 複製 my-example.cnf 至 C:\my.cnf,依提示調整
  3. 啟動:C:\mysql\bin\mysqld-shareware --standalone
  4. 連線建庫:C:\mysql\bin\mysql mysql
  5. 清理匿名帳號:DELETE FROM user WHERE Host='localhost' AND User='';
  6. 重載權限、設 root 密碼:
    mysqladmin reload
    mysqladmin -u root password 'your_pass'
  7. 可選:my.cnf [client] 區段預存帳密

VC++ 6.0 + MySQL C API 開發範例

#include 
#include 
#include 

int main() {
    MYSQL *conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, NULL, "root", "your_pass", "test", MYSQL_PORT, NULL, 0)) {
        fprintf(stderr, "Connect failed: %s\n", mysql_error(conn));
        return 1;
    }

    // 建表
    if (mysql_query(conn, "CREATE TABLE mytable (time DATETIME, s1 CHAR(6), s2 CHAR(11), s3 INT, s4 INT)")) {
        fprintf(stderr, "Create failed: %s\n", mysql_error(conn));
        mysql_close(conn); return 1;
    }

    // 插入
    mysql_query(conn, "INSERT INTO mytable VALUES ('2000-3-10 21:01:30','Test','MySQLTest',2000,3)");

    // 查詢
    mysql_query(conn, "SELECT * FROM mytable");
    MYSQL_RES *res = mysql_store_result(conn);
    int rows = mysql_num_rows(res);
    printf("Query: SELECT * FROM mytable\n%ld records found:\n", rows);

    MYSQL_FIELD *fd;
    char *fld_names[25];
    int i=0;
    while ((fd = mysql_fetch_field(res))) fld_names[i++] = fd->name;

    MYSQL_ROW row;
    while ((row = mysql_fetch_row(res))) {
        for (int k=0; k

關鍵 API 對照

函數用途
mysql_init()初始化 MYSQL 結構
mysql_real_connect()建立連線
mysql_query()執行 SQL
mysql_store_result()取得結果集
mysql_fetch_row()逐行取資料
mysql_free_result()釋放結果集
mysql_close()關閉連線

編譯部署

  • Include:c:\mysql\include
  • Link:c:\mysql\lib\libmysql.lib
  • Runtime:libmysql.dll 複製至 C:\WINNT\SYSTEM32

成果驗證

  • 遷移後:每秒記錄 70-100 條行情、支援 50 並發查詢
  • 開發/維護成本大幅降低,MySQL 免費授權
  • 穩定性獲 Yahoo、網易、263 等大站驗證

結語: Windows NT + MySQL + C API = 低成本、高效能、易維護的黃金組合。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved