douwen4125
douwen4125
2011-02-03 16:49

MySQL,使用LIMIT 0,1800到LIMIT 72000,18000循环(40次)来创建平面文件,必须是更加DB的友好方式吗?

已采纳

I'm creating some flat files from my MySQL database in a php job. Each file is 225kb. I create 40 files.

Basically, I'm running a PHP script which calls a query, the first time it has LIMIT 0, 1800. It then loops and runs 40 times and the last query uses LIMIT 72000,1800.

In the loop I sleep for 0.7 seconds. The whole process takes 45 seconds.

Heres some debug info I produced.

Query took 0.03 second for ../sitemap-0.xml done!
Query took 0.06 second for ../sitemap-1800.xml done!
..snip ..
Query took 0.9 second for ../sitemap-70200.xml done!
Query took 0.9 second for ../sitemap-72000.xml done!
Took: 44.7057

You'll notice the queries take longer and longer as the job runs, this must be the amount of data that the query needs to look at to determine the position of the LIMIT. As a result the CPU usage increases with each query.

The maximum cpu times limit on the server is 60 seconds.

My prime consideration is to keep cpu time and database query time as low as possible. Having high values is unacceptable.

Running the query over and over seems a bit waste of resources.

Is there a better way to do this ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongzhang1875 dongzhang1875 10年前

    The better solution would be to use unbuffered results (so it returns the result without waiting for all of the results to finish transferring) with no limit.

    So, if you're using the mysql extension (using mysql_unbuffered_query ):

    $sql = 'SELECT a, bunch, of, data FROM a_big_table WHERE some_condition';
    $result = mysql_unbuffered_query($sql);
    $data = array();
    $count = 0;
    while ($row = mysql_fetch_assoc($result)) {
        $count++;
        $data[] = $row;
        if ($count >= 1800) {
            storeData($data);
            $data = array();
            $count = 0;
        }
    }
    if ($count > 0) {
        storeData($data);
    }
    

    Where the function storeData actually writes the files.

    The benefit of this, is two fold. First, the query only executes once, so you're not re-running things multiple times. Second, it's unbuffered so you can start fetching results immediately rather than waiting for the whole query to finish.

    点赞 10 评论 复制链接分享
  • dttl3933 dttl3933 10年前

    You can do it faster using MySQL rather than loading the results to PHP:

    SELECT some expression FROM table WHERE condition LIMIT offset, count INTO OUTFILE '/path/to/file.txt'
    

    PHP could look like this:

    for ($i = 0; $i < 72000; $i+=1800) {
        $sql = "SELECT some expression FROM table WHERE condition LIMIT $i, 1800 INTO OUTFILE '/path/to/sitemap-$i.xml'";
        mysql_query($sql);
    }
    
    点赞 评论 复制链接分享
  • drmy1050 drmy1050 10年前

    If you just need the data in XML, then you can do it directly if you have MySQL 5.1+:

    mysql -uyour_user your_database -e "SELECT * FROM table WHERE something > 0" --xml > /path/to/file.xml
    

    If you need to input a user password, add -p after your_user. Obviously change the SELECT * FROM table WHERE something > 0 to your actual query. You can read more about it at this article.

    点赞 评论 复制链接分享

为你推荐