download201401 2016-01-28 16:14 采纳率: 0%
浏览 39

使用php循环加速大sql查询

I'm trying to make these SQL querys faster. I've tried with indexes and so on but I'm abit unsure how to make it faster. Any tips or points are welcome. Its abit old code that an old programmer made, trying to fix it up. It's a MySQL database.

I have two tabels:

wp_l (550 000rows and growing each day) and wp_lP (540 000rows and growing)

Structure:

wp_l:
id - int(9)
epost - (varchar255)
userID - int(9)
date - timestamp
and a few more fields containing info thats not used in this case.

wp_lP:
id - int(9)
lID - int(9) [is the id it belongs to in wp_l, nothing is done in "mysql" to make them relate]
pID - int(9)
quantity - int(9)

My php code:

<?php
    $years = $wpdb->get_results($wpdb->prepare("SELECT YEAR(date) AS year FROM wp_l WHERE userID = %d OR epost = %s GROUP BY year DESC", $current_user->ID, $current_user->user_email));
    if(count($years) == 0) {
        //No result
    } else {
    foreach ( $years as $year ) {

    echo $year->year;

        $myOrders2 = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_l WHERE YEAR(date) = %s AND (userID = %d OR epost = %s) ORDER by id DESC", $year->year, $current_user->ID, $current_user->user_email), ARRAY_N);
        for($i=0; $i<count($myOrders2); $i++) {

        $ar = substr($myOrders2[$i][3], 0, 4);

            $ordersProducts2[$i] = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_lP WHERE lID = %d ORDER by id", $myOrders2[$i][0]), ARRAY_N);

            for($j=0; $j<count($ordersProducts2[$i]); $j++) {
                $loopProductID = $ordersProducts2[$i][$j][2];
                $quantity = $ordersProducts2[$i][$j][3];
                $tid = $myOrders2[$i][3];
                include(locate_template('loopfile.php'));
            } 
        } 
    }
    }
?>

Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题