PINQ-查询数据集-简介

PINQ-查询数据集-简介You may have heard of LINQ Language Integrated Query a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C and Visual Basic

大家好,我是讯享网,很高兴认识大家。

You may have heard of LINQ (Language-Integrated Query), a “set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic”.

您可能听说过LINQ (语言集成查询),这是“ Visual Studio 2008中引入的一组功能,将强大的查询功能扩展到C#和Visual Basic的语言语法”。

It provides necessary interfaces and syntax to perform various queries on a given dataset so that operations like filtering, sorting, grouping, aggregating, etc can be performed.

它提供了必要的接口和语法,可以对给定的数据集执行各种查询,以便可以执行诸如过滤,排序,分组,聚合等操作。

PINQ (PHP Integrated Query) is “based off the .NET’s Linq, and unifies querying across arrays/iterators and external data sources, in a single readable and concise fluent API”. (Excerpted from PINQ’s official site)

PINQ(PHP集成查询)“基于.NET的Linq,并通过单个可读且简洁的API统一了跨阵列/迭代器和外部数据源的查询”。 (摘自PINQ的官方网站 )

为什么要使用另一种查询语言? (Why another query language?)

PHPers are very much comfortable with executing queries with either raw SQL statements or via ORM. We select the data from a database, process it and display it in a chosen format (say, a table form). If we need another set of data, we issue another statement, process the returned dataset and display it.

PHPer非常适合使用原始SQL语句或通过ORM执行查询。 我们从数据库中选择数据,进行处理并以选定的格式(例如表格形式)显示。 如果需要另一组数据,则发出另一条语句,处理返回的数据集并显示它。

In normal cirucumstances, this process is both sufficient and efficient.

在正常情况下,此过程既充分又有效。

But there are cases where this process simply won’t work. Take, for example, a typical e-Commerce website. The user enters one search keyword (say, “router”) and the site displays every matching item. The initial search may only search items with their description, category or tags containing the keyword. Next, the user will start to fine tune the search results by selecting a brand, a price range, etc.

但是在某些情况下,此过程根本行不通。 以一个典型的电子商务网站为例。 用户输入一个搜索关键字(例如“路由器”),该站点将显示每个匹配的项目。 初始搜索只能搜索具有其描述,类别或包含关键字的标签的项目。 接下来,用户将开始通过选择品牌,价格范围等来微调搜索结果。

This fine tuning process is called “faceted” search. Some database engines (like SOLR) have this capability built in (as described in this series: Using Solarium for SOLR Search) but obviously MySQL does not come with this functionality.

这种微调过程称为“分面”搜索。 一些数据库引擎(如SOLR)具有内置的此功能(如本系列中所述: 使用Solarium进行SOLR搜索 ),但显然MySQL并未提供此功能。

That does not mean, however, that MySQL can’t provide such features. After all, it is all about constructing a new SQL statement and fetching the data again. This has some disadvantages, however:

但是,这并不意味着MySQL无法提供此类功能。 毕竟,这都是关于构造新SQL语句并再次获取数据的。 但是,这有一些缺点:

  1. The criteria of the SQL statement, i.e., the “where” and/or “group by” part, can get very complicated after SQL construction.

    SQL语句的条件,即“ where”和/或“ group by”部分,在构建SQL之后会变得非常复杂。
  2. As the SQL statement will be very dynamic, it can’t be optimized and will make indexing more difficult.

    由于SQL语句将是非常动态的,因此无法进行优化,这将使索引编制更加困难。
  3. It will create a huge overhead when communicating the SQL statement back to the db server.


    讯享网

    将SQL语句传递回db服务器时,将产生巨大的开销。

In these cases, PINQ may come in handy. It is a PHP version of the LINQ library which provides filtering, sorting, grouping, aggregating, and indexing on a given dataset.

在这些情况下, PINQ可能会派上用场。 它是LINQ库PHP版本,可对给定的数据集进行过滤,排序,分组,聚合和索引。

制备 (Preparation)

In this series of two parts, we will demonstrate how to use PINQ to mimic a “faceted” search. We will use the sample book collection application’s data (see Data Fixtures in Symfony2 on how to dump the sample data) but with some slight modifications.

在这个由两部分组成的系列文章中,我们将演示如何使用PINQ模仿“分面”搜索。 我们将使用样本书收集应用程序的数据(有关如何转储样本数据的信息,请参见Symfony2中的数据夹具 ),但需进行一些细微修改。

Also, we will use Silex, a very light-weight PHP framework and Bootstrap CSS to simplify the app setup. Please follow the instructions in their respective websites on how to set up a Silex web app and integrate Bootstrap CSS.

另外,我们将使用Silex (一个非常轻巧PHP框架)和Bootstrap CSS来简化应用设置。 请遵循各自网站上的说明,以了解如何设置Silex网络应用程序以及如何集成Bootstrap CSS。

The sample data we used in this demo is slightly modified and I have uploaded it to the repo for this demo. The source code can also be found there.

我们在本演示中使用的样本数据已稍作修改,我已将其上载到该演示的回购中 。 也可以在此处找到源代码。

PINQ安装 (PINQ Installation)

The recommended PINQ installation is to modify the composer.json file that comes with Silex and add one more line in its require section:

推荐的PINQ安装是修改Silex随附的composer.json文件,并在其require节中再添加一行:

{ "require": { "silex/silex": "~1.1", "twig/twig": ">=1.8,<2.0-dev", "doctrine/dbal": "2.2.*", "symfony/twig-bridge": "~2.3", "timetoogo/pinq": "~2.0" } }

讯享网

Please note that I have also added a few more dependencies: Twig (and Twig-Bridge) to display the results, and Doctrine as I am to grab data from my database for further processing.

请注意,我还添加了其他一些依赖项:Twig(和Twig-Bridge)显示结果,而Doctrine则是从数据库中获取数据以进行进一步处理。

After this, we can run a composer.phar update command to install all necessary packages.

之后,我们可以运行composer.phar update命令来安装所有必需的软件包。

演示1:基本用法 (Demo 1: the basic usage)

We will first show a few lines of code to demonstrate the basic usage of PINQ. I will grab the books as they are, do a simple filter (for price between 90 and 99) and then display the aggregation information for different authors.

我们将首先显示几行代码,以演示PINQ的基本用法。 我将按原样抓书,做一个简单的过滤器(价格在90到99之间),然后显示不同作者的汇总信息。

The display will be like this:

显示将如下所示:

alt
小讯
上一篇 2025-02-17 09:26
下一篇 2025-01-13 21:29

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/128281.html