php - CakePHP - 对 JOIN 的结果进行分组和重复数据删除

标签 php mysql sql performance cakephp

我目前正在尝试在我的 CakePHP 站点中实现搜索引擎功能,尝试有效地从 3 个表返回信息。主要用途是数字搜索,自由文本将非常少,因此我不会尝试针对这种情况进行优化。

我遇到的问题是尝试对一个表中的结果进行分组以减少重复信息,很抱歉这篇文章很长!

使用的表格如下:

Companies hasMany Products
Products hasMany Prices

我有一个成功的方法,可以使用以下代码根据任何或所有表的条件从所有 3 个表返回结果(作为问题 here 的结果)

    //configure search conditions
    $options['conditions'] = array(
        'Company.name LIKE' => '%'.$search_term.'%',
        'Product.feature' => $product_feature,
        'Price.price <' => $price
    );

    //configure search fields
    $options['fields'] = array(
        'Company.id',
        'Company.name',
        'Product.id',
        'Product.feature',
        'Price.id',
        'Price.price',
    );

    //configure search joins
    $options['joins'] = array(
        'INNER JOIN prices as Price ON Price.product_id = Product.id INNER JOIN companies as Company ON Product.company_id = Company.id'
    );

    //configure recursion
    $options['recursive'] = -1;

    //configure pagination options
    $this->Paginator->settings = $options;

    //retrieve results and pass to view
    $this->set('results', $this->Paginator->paginate('Product'));

上述查询返回的结果如下:

Array
(
[0] => Array
    (
        [Company] => Array
            (
                [id] => 1
                [name] => Company 1
            )

        [Product] => Array
            (
                [id] => 1
                [feature] => true
            )

        [Price] => Array
            (
                [id] => 1
                [price] => 1.00
            )

    )

[1] => Array
    (
        [Company] => Array
            (
                [id] => 1
                [name] => Company 1
            )

        [Product] => Array
            (
                [id] => 1
                [feature] => true
            )

        [Price] => Array
            (
                [id] => 2
                [price] => 2.00
            )

    )
)

正如您所看到的,上述实例中的公司和产品信息是重复的,理想情况下我希望返回的信息如下:

Array
(
[0] => Array
    (
        [Company] => Array
            (
                [id] => 1
                [name] => Company 1
            )

        [Product] => Array
            (
                [id] => 1
                [feature] => true
            )

        [Price] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [price] => 1.00
                    )
                [1] => Array
                    (       
                        [id] => 2
                        [price] => 2.00
                    )
            )
    )
)

我设法使用以下设置来创建它:

    //configure search joins
    $options['joins'] = array(
        'INNER JOIN prices as Price ON Price.product_id = Product.id'
    );

    //configure recursion
    $options['recursive'] = 1;

上面的代码继续仅返回满足公司和产品所有条件的结果,但在价格数组中,它返回指定公司和产品的所有价格,而不仅仅是满足条件的价格。

例如:具有上述信息的“最高价格为 1”的条件将返回其价格满足条件“最高价格为 1”的所有公司和产品,问题是它只会列出所有价格即使不满足条件,如下:

Array
(
[0] => Array
    (
        [Company] => Array
            (
                [id] => 1
                [name] => Company 1
            )

        [Product] => Array
            (
                [id] => 1
                [feature] => true
            )

        [Price] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [price] => 1.00
                    )
                //the below array result shouldn't be here as it doesn't meet the condition "max price of 1"
                [1] => Array
                    (       
                        [id] => 2
                        [price] => 2.00
                    )
            )
    )
)

问题:如何修改上述代码,以从价格表中返回分组结果的信息,以减少重复,但只返回那些实际满足指定条件的信息?

奖励:如果有更有效的方法来执行上述搜索,我将非常有兴趣知道。尽管上面的操作花费了 0 毫秒,而且我在本地计算机上得到的结果数量有限,CakePHP 仍然告诉我“可能很慢”,我相信这是连接的结果。

最佳答案

将问题一分为二

您所描述的是:

  • 查找至少有一个产品符合条件的所有产品
  • 对于这些产品,请退回具有匹配价格数据的产品。

您描述的关联是:

Company hasMany Product 
Product hasMany Price

或者:

Product belongsTo Company
Price belongsTo Product

这样表达,如果递归为 0 或更大,则很明显,产品上的查找将加入公司。这消除了一个手动加入。

确保退回正确的产品

首先确保您获得所需的产品列表。根据描述,可以选择使用连接进行设置:

$options['recursive'] = 0; // temporary

$options['conditions'] = array(
    'Company.name LIKE' => '%'.$search_term.'%',
    'Product.feature' => $product_feature,
    'Price.price <' => $price
);

//configure search fields
$options['fields'] = array(
    'Distinct Product.id',
    'Product.feature',
    'Company.id',
    'Company.name',
    #'Price.id', No
    #'Price.price', No
);

$options['joins'][] = 'INNER JOIN prices as PriceFilter ON Price.product_id = Product.id';

或者条件:

$options['recursive'] = 0; // temporary

$options['conditions'] = array(
    'Company.name LIKE' => '%'.$search_term.'%',
    'Product.feature' => $product_feature,
    "WHERE EXISTS (select * from prices where prices.product_id = Product.id AND prices.price < $price)"
);

$options['fields'] = array(
    'Product.id',
    'Product.feature',
    'Company.id',
    'Company.name',
    #'Price.id', No
    #'Price.price', No
);

请注意,主查找/分页调用中现在没有额外的联接。

在这两个示例中,都应该执行一个查询(加上计数),没有任何价格数据。

使用containable获取匹配价格

Containable可以更轻松地管理执行的查询以及返回的结果范围。在这种情况下,所需要做的就是将价格数据添加到结果集中 - 并过滤价格。演示 contain 的使用的完整示例选项:

public $paginate = array(
    'contain' => array(
        'Company',
        'Price' => array()
    ),
    'fields' => array(
        'Product.id',
        'Product.feature',
        'Company.id',
        'Company.name'
    )
);

function whatever() {
    ...

    $this->paginate['contain']['Price']['conditions']['Price.price <'] = $price;

    $conditions = array(
        'Company.name LIKE' => '%'.$search_term.'%',
        'Product.feature' => $product_feature,
        "WHERE EXISTS (select * from prices where prices.product_id = Product.id AND prices.price < $price)"
    );

    $result = $this->paginate('Product', $conditions);

    ...
}

这应该会产生两个个查询(加上一个计数)以及您要查找的数据结构;包括价格数据。

可能慢

Even though the above takes 0ms, [...] CakePHP is still telling me "maybe slow"

调试工具包不会询问数据库,以确定查询是否“可能很慢”,它是 simple test用于:

  • 查询耗时超过 0 毫秒
  • 每个结果的查询时间超过 1 毫秒
  • 查询时间超过阈值(默认为 20 毫秒)

从检查代码来看,它永远不会将 0ms 查询标记为“可能很慢” - 但如果确实如此,也不是问题。

与所有数据库事件一样,最好在数据库上运行解释,添加任何缺失的索引并考虑将返回相同数据的不同查询结构。

关于php - CakePHP - 对 JOIN 的结果进行分组和重复数据删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375649/

相关文章:

php - 5 秒后自动关闭 HTML 页面

php - REST API 在插件域中不工作

php - 自动维护/"We will be back"页

MySQL - 如何索引该查询?

java - 您的 SQL 语法有误;查看与您的 MySQL 服务器对应的手册

php - 如何通过 PHP 将 JSON 对象数组添加到 MySQL 中

php - 无法在使用 php 的 mysql 获取查询中使用变量

python - 添加新数据库条目时如何触发页面刷新(重新加载)?

java - java中的时间戳作为文件名

java - 使用 Hibernate 更新两个不相关的表,如果任何事务失败,我们应该回滚这两个事务吗?