码迷,mamicode.com
首页 > 数据库 > 详细

MySQL分页 limit 优化

时间:2021-05-24 14:29:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:limit   size   mes   res   ted   exist   auto   lap   asc   

  

 

  1. 子查询
    技术图片 

    记录        [3,+2)           从0编号,第三行记录序号为 2

    limit offset

    技术图片

     

     
    子查询

    技术图片

     

     子查询查出第三行记录的dept_no


    导表sql
    技术图片
    -- MySQL dump 10.13  Distrib 8.0.22, for Win64 (x86_64)
    --
    -- Host: localhost    Database: employees
    -- ------------------------------------------------------
    -- Server version    8.0.22
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!50503 SET NAMES utf8mb4 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE=‘+00:00‘ */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
    --
    -- Table structure for table `departments`
    --
    
    DROP TABLE IF EXISTS `departments`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!50503 SET character_set_client = utf8mb4 */;
    CREATE TABLE `departments` (
      `dept_no` char(4) NOT NULL,
      `dept_name` varchar(40) NOT NULL,
      PRIMARY KEY (`dept_no`),
      UNIQUE KEY `dept_name` (`dept_name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `departments`
    --
    
    LOCK TABLES `departments` WRITE;
    /*!40000 ALTER TABLE `departments` DISABLE KEYS */;
    INSERT INTO `departments` VALUES (d009,Customer Service),(d005,Development),(d002,Finance),(d003,Human Resources),(d001,Marketing),(d004,Production),(d006,Quality Management),(d008,Research),(d007,Sales);
    /*!40000 ALTER TABLE `departments` ENABLE KEYS */;
    UNLOCK TABLES;
    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    
    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    
    -- Dump completed on 2021-05-18  9:50:05
    View Code

     

  2. join
    SELECT
        * 
    FROM
        departments t1
        JOIN ( SELECT dept_no FROM departments ORDER BY dept_no ASC LIMIT 2, 1 ) t2 
    WHERE
        t1.dept_no >= t2.dept_no 
    ORDER BY
        t1.dept_no ASC 
        LIMIT 2

    子查询在索引上完成

MySQL分页 limit 优化

标签:limit   size   mes   res   ted   exist   auto   lap   asc   

原文地址:https://www.cnblogs.com/dissipate/p/14780146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!