首页 > 其他分享 >关于SELECT的疑惑

关于SELECT的疑惑

时间:2022-08-21 13:22:40浏览次数:82  
标签:疑惑 10 database 关于 table query SELECT view

以mysql为例进行分页,如:

  • SELECT * FROM table WHERE age < 30 LIMIT 0, 10
  • SELECT * FROM table WHERE age < 30 LIMIT 10, 20
  • ...
  • SELECT * FROM table WHERE age < 30 LIMIT 1000, 1010

数据库如何直接从第1000条开始?自动把"SELECT * FROM table WHERE age < 30"的结果缓存起来?View?

从14亿行中选age < 30的,比如结果有2亿行。在PHP里:

$result = mysqli_query($connection, $sql); $row_count = mysqli_num_rows($result);

数据库是不是要用很长时间才能返回row_count?用程序打开文本文件逐行读并输出满足条件的行,可能马上就看到第一行,而总数要最后才知道。

虽然这里讲了不少SQL,我还是不明白。

A SELECT statement retrieves zero or more rows from one or more database tables or database views. The database translates the query into a "query plan" which may vary between executions, database versions and database software. This functionality is called the "query optimizer" as it is responsible for finding the best possible execution plan for the query, within applicable constraints.

In a database, a view is the result set of a stored query on the data, which the database users can query just as they would in a persistent database collection object. This pre-established query command is kept in the database dictionary. Unlike ordinary base tables in a relational database, a view does not form part of the physical schema: as a result set, it is a virtual table computed or collated dynamically from data in the database when access to that view is requested. Changes applied to the data in a relevant underlying table are reflected in the data shown in subsequent invocations of the view.

A query plan (or query execution plan) is a sequence of steps used to access data in a SQL relational database management system. Since SQL is declarative, there are typically many alternative ways to execute a given query, with widely varying performance. When a query is submitted to the database, the query optimizer evaluates some of the different, correct possible plans for executing the query and returns what it considers the best option. Because query optimizers are imperfect, database users and administrators sometimes need to manually examine and tune the plans produced by the optimizer to get better performance.

The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plans. Generally, the query optimizer cannot be accessed directly by users: once queries are submitted to the database server, and parsed by the parser, they are then passed to the query optimizer where optimization occurs. However, some database engines allow guiding the query optimizer with hints.

Queries can be nested so that the results of one query can be used in another query via a relational operator or aggregation function. A nested query is also known as a subquery. While joins and other table operations provide computationally superior (i.e. faster) alternatives in many cases, the use of subqueries introduces a hierarchy in execution that can be useful or necessary. In the following example, the aggregation function AVG receives as input the result of a subquery:
SELECT isbn, title, price FROM Book WHERE price < (SELECT AVG(price) FROM Book) ORDER BY title;

An Inline view is the use of referencing an SQL subquery in a FROM clause. Essentially, the inline view is a subquery that can be selected from or joined to. Inline View functionality allows the user to reference the subquery as a table. The inline view also is referred to as a derived table or a subselect. In the following example, the SQL statement involves a join from the initial Books table to the Inline view "Sales". This inline view captures associated book sales information using the ISBN to join to the Books table. As a result, the inline view provides the result set with additional columns (the number of items sold and the company that sold the books):

SELECT b.isbn, b.title, b.price, sales.items_sold, sales.company_nm FROM
Book b
JOIN (SELECT SUM(Items_Sold) Items_Sold, Company_Nm, ISBN FROM Book_Sales GROUP BY Company_Nm, ISBN) sales ON sales.isbn = b.isbn

Limiting result rows. Often it is convenient to indicate a maximum number of rows that are returned. This can be used for testing or to prevent consuming excessive resources if the query returns more information than expected. The approach to do this often varies per vendor. In ISO SQL:2003, result sets may be limited by using cursors, or by adding a SQL window function to the SELECT-statement. ISO SQL:2008 introduced the FETCH FIRST clause.

Some DBMSes offer non-standard syntax either instead of or in addition to SQL standard syntax. Below, variants of the simple limit query for different DBMSes are listed:

  • SET ROWCOUNT 10 SELECT * FROM T
  • SELECT * FROM T LIMIT 10 OFFSET 20
  • SELECT * from T WHERE ROWNUM <= 10
  • SELECT FIRST 10 * from T
  • SELECT FIRST 10 * FROM T order by a
  • SELECT SKIP 20 FIRST 10 * FROM T order by c, d
  • SELECT TOP 10 * FROM T
  • SELECT * FROM T SAMPLE 10
  • SELECT TOP 20, 10 * FROM T
  • SELECT TOP 10 START AT 20 * FROM T
  • SELECT FIRST 10 SKIP 20 * FROM T
  • SELECT * FROM T ROWS 20 TO 30
  • SELECT * FROM T WHERE ID_T > 10 FETCH FIRST 10 ROWS ONLY
  • SELECT * FROM T WHERE ID_T > 20 FETCH FIRST 10 ROWS ONLY

Rows Pagination is an approach used to limit and display only a part of the total data of a query in the database. Instead of showing hundreds or thousands of rows at the same time, the server is requested only one page (a limited set of rows, per example only 10 rows), and the user starts navigating by requesting the next page, and then the next one, and so on. It is very useful, specially in web systems, where there is no dedicated connection between the client and the server, so the client does not have to wait to read and display all the rows of the server.

sql查询(三)之分页查询 | PHP mysqli_num_rows()

六级/考研单词: retrieve, nil, data, translate, query, hardware, execute, persist, dictionary, physics, compute, dynamic, underlie, alternate, evaluate, manual, tune, hint, nest, aggregate, superior, hierarchy, necessity, derive, seldom, consume, excess, vendor, offset, skip, navigate, web, dedicate

标签:疑惑,10,database,关于,table,query,SELECT,view
From: https://www.cnblogs.com/funwithwords/p/16609849.html

相关文章

  • 系列文章目录和关于我
    系列文章目录和关于我A丶系列文章目录一丶JUC源码系列本系列从AQS独占,共享,等待队列依次推进,并且穿插的分析了常见工具如ReentrantLock,CountDownLatch,原子类,FutureTask,Th......
  • 关于DolphinScheduler
    ApacheDolphinScheduler是一个分布式易扩展的可视化DAG工作流任务调度开源系统。适用于企业级场景,提供了一个可视化操作任务、工作流和全生命周期数据处理过程的解决方案......
  • 关于layui表格中列的宽度问题怎么修改
    <divclass="layui-card-body"><tableclass="layui-table"><!--这个colgroup就是来控制表格里面每一列的宽度,不加参数就是表明自适应--......
  • ubuntu关于防火墙开放端口
    一、防火墙1、查看防火墙状态sudoufwstatus1系统提示:“Status:inactive”状态:不活跃上面提示表示没有开启防火墙,并不是没有安装防火墙注:如果没有安装防火墙,可以使用......
  • 关于GreenPlum的构架
    Greenplum数据库是一种大规模并行处理(MPP)数据库服务器,其架构特别针对管理大规模分析型数据仓库以及商业智能工作负载而设计。 MPP(也被称为sharednothing架构)指有两个或......
  • 关于 Knex update 语句的 where 子句出现 Undefined binding(s) detected when compil
    因为是第一次使用Knex操作数据库来开发接口,一个业务中用到了update语句,且有where子句。下图是详细的报错截图,这里保证前端一个不漏的把需要的字段都传递过来了,但还......
  • 关于异常处理
     1-常规a.try不可以单独存在,要么有catch,要么有finally其中,StackTrace很棒,可以查看问题处在第几行。2-关于finallya.即使有return,依然要走finally的代码语句b.try......
  • onclick 触发 select展开
    https://zhidao.baidu.com/question/587667483.htmlsize方法比较简单,除了这个就只有模拟操作了。1234567<select size="1" name="D1" id="test1"> ......
  • 一些关于Viewport与device-width的东西~
    https://www.cnblogs.com/koukouyifan/p/4066567.html进行移动web开发已经有一年多的时间了,期间遇到了一些令人很困惑的东西。比如:我们经常使用的<metaname="viewpor......
  • 关于appendChild报错
    今天在做尚优选html页面时,将一个<li>添加到一个<ul>中使用appendChild(li)报出Cannotreadproperty'appendChild'ofnull异常报错原因:获取不到元素节点从而导致的异常......