MySQL分页 limit 优化

  

 

  1. 子查询
    MySQL分页 limit 优化 

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

    limit offset

    MySQL分页 limit 优化

     

     
    子查询

    MySQL分页 limit 优化

     

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


    导表sql
    MySQL分页 limit 优化
    -- 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

    子查询在索引上完成

上一篇:文件操作


下一篇:ubuntu软件源设置