douyakao5308 2017-04-05 16:17
浏览 56

最小化SQL语句? - 堆栈SQL查询?

I'm working on a WordPress Plugin script which extends the plugin. So, my problem is, I have to deal with the database which is pretty inefficient because I have only the first normal form...

This is the reason, why I'm forced to get the values like this:

$details = $database->query("SELECT `meta_key`, `meta_value` FROM `6v8T0_postmeta` WHERE post_id = '$pid' AND (meta_key like '$price_meta' OR meta_key like '$price_old_meta' OR meta_key like '$link_meta' OR meta_key like '$shop_meta')");

        while($row_meta = $details->fetch_assoc()){
            if($row_meta["meta_key"] == $price_meta){
                $price = $row_meta["meta_value"];
            }elseif($row_meta["meta_key"] == $price_old_meta){
                $price_old = $row_meta["meta_value"];
            }elseif($row_meta["meta_key"] == $link_meta){
                $link = $row_meta["meta_value"];
            }elseif($row_meta["meta_key"] == $shop_meta){
                $shop = $row_meta["meta_value"];
            }else{
                fwrite($myfile, "Is not matching!
");
            }
        }
  1. My question now is, what is $database->query returning? Because I want to minimize my SQL queries I want to load all values I need in a two dimensional array and sort them afterwords.

Is that possible and if yes, how?

  1. Stack SQL queries? - I say multiple times Select, Delete and Insert in a loop. Is it possible to stack those statements and execute one query instant of 4? - And if yes, can I continue in my code with fetch_assoc() normally? - Because I'm not sure if this is possible if I stack Delete, Insert and Select...

  2. Are there any other ways to minimize SQL queries?

Greetings and Thank You!

  • 写回答

1条回答 默认 最新

  • duanping2005 2017-04-05 16:41
    关注

    First the database structure isn't inefficient, it's a widely used and accepted pattern of storing a list of key/value pairs in the database.

    There are two ways you can improve the situation for yourself.

    MAPPING ARRAY

    This one is probably going to be your best choice, as it's very flexible and doesn't require you to change your queries.

    $details = $database->prepare("
        SELECT
            `meta_key`,
            `meta_value`
        FROM
            `6v8T0_postmeta`
        WHERE
            post_id = '$pid'
            AND meta_key IN (
                %s,
                %s,
                %s,
                %s
            )
        ",
        array(
            $price_meta,
            $price_old_meta,
            $link_meta,
            $shop_meta
       )
    );
    
    $map = array();
    
    while($row_meta = $details->fetch_assoc()){
        $map[$row_meta['meta_key']] = $row_meta['meta_value'];
    }
    
    /*
    
    $map will look something like this
    
    array(
        'price' => '10.99',
        'price_old' => '9.99',
        'link' => 'http://example.org/awesome-product',
        'shop' => 'main'
    )
    
    */
    
    $price = array_key_exists($price_meta, $map) ? $map[$price_meta] : 'default value';
    $price_old = array_key_exists($price_old_meta, $map) ? $map[$price_old_meta] : 'default value';
    $link = array_key_exists($link_meta, $map) ? $map[$link_meta] : 'default value';
    $shop = array_key_exists($shop_meta, $map) ? $map[$shop_meta] : 'default value';
    

    JOINS

    This method makes your query long and needs you to update it everytime you want a new field added.

    $details = $database->prepare("
        SELECT
            b.meta_value %s,
            c.meta_value %s,
            d.meta_value %s,
            e.meta_value %s
        FROM
            `6v8T0_postmeta` a
        LEFT JOIN
            `6v8T0_postmeta` b ON
                b.post_id = a.post_id
                AND b.meta_key = %s
        LEFT JOIN
            `6v8T0_postmeta` c ON
                c.post_id = a.post_id
                AND c.meta_key = %s
        LEFT JOIN
            `6v8T0_postmeta` d ON
                d.post_id = a.post_id
                AND d.meta_key = %s
        LEFT JOIN
            `6v8T0_postmeta` e ON
                e.post_id = a.post_id
                AND e.meta_key = %s
        WHERE
            a.post_id = '$pid'
        ",
        array(
            $price_meta,
            $price_old_meta,
            $link_meta,
            $shop_meta,
            $price_meta,
            $price_old_meta,
            $link_meta,
            $shop_meta,
            $price_meta,
            $price_old_meta,
            $link_meta,
            $shop_meta
        )
    );
    
    $map = array();
    
    if (!($row_meta = $details->fetch_assoc())) {
        $price = $row_meta[$price_meta];
        $price_old = $row_meta[$price_old_meta];
        $link = $row_meta[$link_meta];
        $shop = $row_meta[$shop_meta];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog