承接 datajoe/postgresql-distinct-on 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

datajoe/postgresql-distinct-on

Composer 安装命令:

composer require datajoe/postgresql-distinct-on

包简介

Provides support for the postgresql specific distinct on feature for Laravel. Compatible with versions 5 through 8.

README 文档

README

What it does:

  • Provides support for the postgresql specific DISTINCT ON feature for Laravel 5-9.

From the Postgresql documentation:

SELECT DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). Note that the "first row" of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first.

For example:

SELECT DISTINCT ON (location) location, time, report
   FROM weather_reports
   ORDER BY location, time DESC;

retrieves the most recent weather report for each location. But if we had not used ORDER BY to force descending order of time values for each location, we'd have gotten a report from an unpredictable time for each location.

The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). The ORDER BY clause will normally contain additional expression(s) that determine the desired precedence of rows within each DISTINCT ON group.

Why do we need it?

I built this specifically because I wanted the ability to have a descriptive metadata table that allowed me to retain all versions of data for a given key. I needed to be able to easily retrieve only the most recent version of the stored value. Postgres makes this very easy by supporting SELECT DISTINCT ON, however Laravel/Eloquent have no base support for this database specific functionality.

How to use it:

  1. In your project directory, run: composer require datajoe/postgresql-distinct-on

  2. In your app.php, replace Illuminate\Database\DatabaseServiceProvider::class, with DataJoe\Extensions\Illuminate\Database\DatabaseServiceProvider::class,

  3. In your code, you can access the DISTINCT ON functionality using the ->distinctOn('field_name') method. Be sure to also include ->orderBy('field_name') in your query!

Example:

This would return a single RecordMeta row, with only the most recent update_at timestamp, for each distinct meta_name where the record_id is 1

$fields = RecordMeta::select(['record_id', 'value', 'updated_at'])
                 ->distinctOn('meta_name')
                 ->where('record_id', 1)
                 ->orderBy('meta_name')
                 ->orderBy('updated_at', 'desc');

That command would be equivalent to the following sql:

SELECT DISTINCT ON ('meta_name') meta_name, record_id, value, updated_at
    FROM record_meta
    WHERE record_id = 1
    ORDER BY meta_name, updated_at DESC;

Other cool things you can do:

To be able to access the meta data through a model relationship (i.e. Record -> RecordMeta), you can add the following to your record model to establish a custom relationship:

public function recordMeta()
{
    $relation = $this->hasMany(RecordMeta::class);

    $relation->getQuery()
             ->select(['record_id', 'value', 'updated_at'])
             ->distinctOn('meta_name')
             ->orderBy('updated_at', 'desc');

    return $relation;
}

Then, in your code, you can access the metadata like this:

$record = Record::with('recordMeta')->get();

And you will get back all record with only the most recently set value for each meta_name for each record.

datajoe/postgresql-distinct-on 适用场景与选型建议

datajoe/postgresql-distinct-on 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.92k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「postgresql」 「postgres」 「laravel」 「psql」 「eloquent」 「datajoe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 datajoe/postgresql-distinct-on 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 datajoe/postgresql-distinct-on 我们能提供哪些服务?
定制开发 / 二次开发

基于 datajoe/postgresql-distinct-on 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-25