新用户注册恢复喜悦村十周年

[原创]推广SQLite数据库的运用-最简单的计数器

[PHP]
<?php

/*
sqlite是一个小巧捆绑型单机数据库,优点有:
·支持并发线程,和Ms access的单线程比,可以支持几千人在线的论坛(Access只能支持20个人)。
·单个数据库文件最大2TB=2048GB
·跨平台支持数据库可在各种操作系统移植。
·php4/5都内置了sqlite操作函数,php5用内置extension扩展,php4下载一个extension就行。
·速度快,与单机同时运行mysql和apache两项服务比,sqlite的操作是直接磁盘操作,存取飞快。
适合:没有数据库服务器又需要数据库的php用户,比如廉价的虚拟空间。
和用不上使用Mysql等企业级数据库的用户,比如单机服务器,个人ADSL服务器。

IMKOW 原创!
*/

function MyCounter()
{

/*第一句打开并确认数据库文件hamlet.sqlite,可以由命令行工具命令“sqlite3 hamlet.sqlite"生成或者使用SQL语言:
CREAT hamlet.sqlite
CREATE TABLE 计数器 (id INTEGER PRIMARY KEY, 点击数 INTEGER)

*/

        if($Mydatabase = sqlite_open($_SERVER["SCRIPT_PATH"]."/sqlite/hamlet.sqlite",0666,$sqliteerror))
        //尝试打开数据库,打不开则出错。
        {
                $SQL="SELECT 点击数 FROM 计数器 WHERE id=1";//定位于点击数的第一个记录
                $result=sqlite_query($Mydatabase, $SQL);//查询数据库
                $Counter=sqlite_fetch_single($result);//取得第一个值

                $Counter++;//增量
                //
                $SQL="UPDATE 计数器 SET 点击数=".$Counter." WHERE id=1";//数据库更新
                sqlite_exec($Mydatabase,$SQL);////数据库更新执行
        } else
        {
                die($sqliteerror);//出错返回值
        }
        return $Counter;
}

echo "点击次数:".MyCounter();//调用计数器
?>
[/PHP]
三大常用开源数据库PostgreSQL,MySQL:,SQLite 2.7.6速度对比

Test 1: 1000 INSERTs  测试1:插入1000个记录
CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));
INSERT INTO t1 VALUES(1,13153,'thirteen thousand one hundred fifty three');
INSERT INTO t1 VALUES(2,75560,'seventy five thousand five hundred sixty');
... 995 lines omitted
INSERT INTO t1 VALUES(998,66289,'sixty six thousand two hundred eighty nine');
INSERT INTO t1 VALUES(999,24322,'twenty four thousand three hundred twenty two');
INSERT INTO t1 VALUES(1000,94142,'ninety four thousand one hundred forty two');

PostgreSQL:    4.373
MySQL:    0.114
SQLite 2.7.6:    13.061
SQLite 2.7.6 (nosync):    0.223

Because it does not have a central server to coordinate access, SQLite must close and reopen the database file, and thus invalidate its cache, for each transaction. In this test, each SQL statement is a separate transaction so the database file must be opened and closed and the cache must be flushed 1000 times. In spite of this, the asynchronous version of SQLite is still nearly as fast as MySQL. Notice how much slower the synchronous version is, however. SQLite calls fsync() after each synchronous transaction to make sure that all data is safely on the disk surface before continuing. For most of the 13 seconds in the synchronous test, SQLite was sitting idle waiting on disk I/O to complete.

Test 2: 25000 INSERTs in a transaction 测试二25000行记录一次插入
BEGIN;
CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));
INSERT INTO t2 VALUES(1,59672,'fifty nine thousand six hundred seventy two');
... 24997 lines omitted
INSERT INTO t2 VALUES(24999,89569,'eighty nine thousand five hundred sixty nine');
INSERT INTO t2 VALUES(25000,94666,'ninety four thousand six hundred sixty six');
COMMIT;

PostgreSQL:    4.900
MySQL:    2.184
SQLite 2.7.6:    0.914
SQLite 2.7.6 (nosync):    0.757

When all the INSERTs are put in a transaction, SQLite no longer has to close and reopen the database or invalidate its cache between each statement. It also does not have to do any fsync()s until the very end. When unshackled in this way, SQLite is much faster than either PostgreSQL and MySQL.

Test 3: 25000 INSERTs into an indexed table 测试三:25000行记录插入一个引索后的表
BEGIN;
CREATE TABLE t3(a INTEGER, b INTEGER, c VARCHAR(100));
CREATE INDEX i3 ON t3(c);
... 24998 lines omitted
INSERT INTO t3 VALUES(24999,88509,'eighty eight thousand five hundred nine');
INSERT INTO t3 VALUES(25000,84791,'eighty four thousand seven hundred ninety one');
COMMIT;

PostgreSQL:    8.175
MySQL:    3.197
SQLite 2.7.6:    1.555
SQLite 2.7.6 (nosync):    1.402

There were reports that SQLite did not perform as well on an indexed table. This test was recently added to disprove those rumors. It is true that SQLite is not as fast at creating new index entries as the other engines (see Test 6 below) but its overall speed is still better.

Test 4: 100 SELECTs without an index 测试四 选择输出100项无引索记录
BEGIN;
SELECT count(*), avg(b) FROM t2 WHERE b>=0 AND b<1000;
SELECT count(*), avg(b) FROM t2 WHERE b>=100 AND b<1100;
... 96 lines omitted
SELECT count(*), avg(b) FROM t2 WHERE b>=9800 AND b<10800;
SELECT count(*), avg(b) FROM t2 WHERE b>=9900 AND b<10900;
COMMIT;

PostgreSQL:    3.629
MySQL:    2.760
SQLite 2.7.6:    2.494
SQLite 2.7.6 (nosync):    2.526

This test does 100 queries on a 25000 entry table without an index, thus requiring a full table scan. Prior versions of SQLite used to be slower than PostgreSQL and MySQL on this test, but recent performance enhancements have increased its speed so that it is now the fastest of the group.

Test 5: 100 SELECTs on a string comparison 测试五:输出100行查询文本
BEGIN;
SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%one%';
SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%two%';
... 96 lines omitted
SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%ninety nine%';
SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%one hundred%';
COMMIT;

PostgreSQL:    13.409
MySQL:    4.640
SQLite 2.7.6:    3.362
SQLite 2.7.6 (nosync):    3.372

This test still does 100 full table scans but it uses uses string comparisons instead of numerical comparisions. SQLite is over three times faster than PostgreSQL here and about 30% faster than MySQL.

Test 6: Creating an index 测试六:创建一个引索
CREATE INDEX i2a ON t2(a);
CREATE INDEX i2b ON t2(b);
PostgreSQL:    0.381
MySQL:    0.318
SQLite 2.7.6:    0.777
SQLite 2.7.6 (nosync):    0.659

SQLite is slower at creating new indices. This is not a huge problem (since new indices are not created very often) but it is something that is being worked on. Hopefully, future versions of SQLite will do better here.

Test 7: 5000 SELECTs with an index 测试七:5000行引索选择
SELECT count(*), avg(b) FROM t2 WHERE b>=0 AND b<100;
SELECT count(*), avg(b) FROM t2 WHERE b>=100 AND b<200;
SELECT count(*), avg(b) FROM t2 WHERE b>=200 AND b<300;
... 4994 lines omitted
SELECT count(*), avg(b) FROM t2 WHERE b>=499700 AND b<499800;
SELECT count(*), avg(b) FROM t2 WHERE b>=499800 AND b<499900;
SELECT count(*), avg(b) FROM t2 WHERE b>=499900 AND b<500000;

PostgreSQL:    4.614
MySQL:    1.270
SQLite 2.7.6:    1.121
SQLite 2.7.6 (nosync):    1.162

All three database engines run faster when they have indices to work with. But SQLite is still the fastest.

Test 8: 1000 UPDATEs without an index 测试八:无引索1000项更新
BEGIN;
UPDATE t1 SET b=b*2 WHERE a>=0 AND a<10;
UPDATE t1 SET b=b*2 WHERE a>=10 AND a<20;
... 996 lines omitted
UPDATE t1 SET b=b*2 WHERE a>=9980 AND a<9990;
UPDATE t1 SET b=b*2 WHERE a>=9990 AND a<10000;
COMMIT;

PostgreSQL:    1.739
MySQL:    8.410
SQLite 2.7.6:    0.637
SQLite 2.7.6 (nosync):    0.638

For this particular UPDATE test, MySQL is consistently five or ten times slower than PostgreSQL and SQLite. I do not know why. MySQL is normally a very fast engine. Perhaps this problem has been addressed in later versions of MySQL.

Test 9: 25000 UPDATEs with an index 测试九:25000项引索后更新
BEGIN;
UPDATE t2 SET b=468026 WHERE a=1;
UPDATE t2 SET b=121928 WHERE a=2;
... 24996 lines omitted
UPDATE t2 SET b=35065 WHERE a=24999;
UPDATE t2 SET b=347393 WHERE a=25000;
COMMIT;

PostgreSQL:    18.797
MySQL:    8.134
SQLite 2.7.6:    3.520
SQLite 2.7.6 (nosync):    3.104

As recently as version 2.7.0, SQLite ran at about the same speed as MySQL on this test. But recent optimizations to SQLite have more than doubled speed of UPDATEs.

Test 10: 25000 text UPDATEs with an index 测试十:25000项文本引索后更新
BEGIN;
UPDATE t2 SET c='one hundred forty eight thousand three hundred eighty two' WHERE a=1;
UPDATE t2 SET c='three hundred sixty six thousand five hundred two' WHERE a=2;
... 24996 lines omitted
UPDATE t2 SET c='three hundred eighty three thousand ninety nine' WHERE a=24999;
UPDATE t2 SET c='two hundred fifty six thousand eight hundred thirty' WHERE a=25000;
COMMIT;

PostgreSQL:    48.133
MySQL:    6.982
SQLite 2.7.6:    2.408
SQLite 2.7.6 (nosync):    1.725

Here again, version 2.7.0 of SQLite used to run at about the same speed as MySQL. But now version 2.7.6 is over two times faster than MySQL and over twenty times faster than PostgreSQL.

In fairness to PostgreSQL, it started thrashing on this test. A knowledgeable administrator might be able to get PostgreSQL to run a lot faster here by tweaking and tuning the server a little.

Test 11: INSERTs from a SELECT
BEGIN;
INSERT INTO t1 SELECT b,a,c FROM t2;
INSERT INTO t2 SELECT b,a,c FROM t1;
COMMIT;
PostgreSQL:    61.364
MySQL:    1.537
SQLite 2.7.6:    2.787
SQLite 2.7.6 (nosync):    1.599

The asynchronous SQLite is just a shade slower than MySQL on this test. (MySQL seems to be especially adept at INSERT...SELECT statements.) The PostgreSQL engine is still thrashing - most of the 61 seconds it used were spent waiting on disk I/O.

Test 12: DELETE without an index
DELETE FROM t2 WHERE c LIKE '%fifty%';
PostgreSQL:    1.509
MySQL:    0.975
SQLite 2.7.6:    4.004
SQLite 2.7.6 (nosync):    0.560

The synchronous version of SQLite is the slowest of the group in this test, but the asynchronous version is the fastest. The difference is the extra time needed to execute fsync().

Test 13: DELETE with an index
DELETE FROM t2 WHERE a>10 AND a<20000;
PostgreSQL:    1.316
MySQL:    2.262
SQLite 2.7.6:    2.068
SQLite 2.7.6 (nosync):    0.752

This test is significant because it is one of the few where PostgreSQL is faster than MySQL. The asynchronous SQLite is, however, faster then both the other two.

Test 14: A big INSERT after a big DELETE
INSERT INTO t2 SELECT * FROM t1;
PostgreSQL:    13.168
MySQL:    1.815
SQLite 2.7.6:    3.210
SQLite 2.7.6 (nosync):    1.485

Some older versions of SQLite (prior to version 2.4.0) would show decreasing performance after a sequence of DELETEs followed by new INSERTs. As this test shows, the problem has now been resolved.

Test 15: A big DELETE followed by many small INSERTs
BEGIN;
DELETE FROM t1;
INSERT INTO t1 VALUES(1,10719,'ten thousand seven hundred nineteen');
... 11997 lines omitted
INSERT INTO t1 VALUES(11999,72836,'seventy two thousand eight hundred thirty six');
INSERT INTO t1 VALUES(12000,64231,'sixty four thousand two hundred thirty one');
COMMIT;

PostgreSQL:    4.556
MySQL:    1.704
SQLite 2.7.6:    0.618
SQLite 2.7.6 (nosync):    0.406

SQLite is very good at doing INSERTs within a transaction, which probably explains why it is so much faster than the other databases at this test.

Test 16: DROP TABLE
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
PostgreSQL:    0.135
MySQL:    0.015
SQLite 2.7.6:    0.939
SQLite 2.7.6 (nosync):    0.254
SQLite is slower than the other databases when it comes to dropping tables. This probably is because when SQLite drops a table, it has to go through and erase the records in the database file that deal with that table. MySQL and PostgreSQL, on the other hand, use separate files to represent each table so they can drop a table simply by deleting a file, which is much faster.

On the other hand, dropping tables is not a very common operation so if SQLite takes a little longer, that is not seen as a big problem.
最简单的计数器应该是用文件的吧,俺觉得
不过还是支持SQLite
它为在不提供DBM的情况下使用数据库提供了一种可能
文本的也多2行代码,数据库的计数器多好啊。:>