请大神来讲讲数据库数据量大的时候避免用join查询的方式来做数据库查询优化
boss说平时查询数据量大的数据尽量避免用join 宁可一次将一张表的数据查出来再用这些数据去做查询 也不要用过多的使用join 尽量分成多次查询来做 ,请大神来讲讲这其中的sql优化
回复内容:
boss说平时查询数据量大的数据尽量避免用join 宁可一次将一张表的数据查出来再用这些数据去做查询 也不要用过多的使用join 尽量分成多次查询来做 ,请大神来讲讲这其中的sql优化
如果用了关系型数据库,用表的join是很自然的做法,除非一些特例:
sql语句太复杂,或统计信息不准确,造成数据库生成的执行计划不正确,导致运行效率低下,且短时间没办法改写sql
数据量特别大(至少过亿),数据库负载成为整个系统的性能瓶颈,在架构设计的时候就规定了不用数据库本身的表连接功能。
使用了ORM框架,对于关联对象默认会使用N+1查询的方式,如果数据量不是特别大,加上有缓存功能,效率并不低。
所以一般情况下,用数据库的join功能,比自己多次取数据做关联效率要高,否则一张大数据量表,但是传递到PHP中,就要花费很长的时间。
sql优化最基本的原则,就是让数据库尽早、高效率的过滤数据,避免无效的运算,具体的手段比较多,需要根据不同的数据库来确定实现方案。
请自行explain
之。
这要看你数据库链接是不是长链接了,如果配置为长链接的话还好一点,不过要join的时候可以首先通过where或者其他方法缩减你的主表,不要一股脑的拉起来,这样的话性能会好很多。但是还是具体问题具体分析吧,跑一遍就有结果了哇。不过如果不是长链接的话估计连表会快一点。
分成多次查询的原因是因为:无论是join还是子查询,mysql优化器都会对sql进行优化(子查询就会自作聪明办坏事,join偶尔也会)
所以一条一条写,自己可以把握
join方式其实挺好,主要是索引把握起来不是那么的得心应手,容易出现问题。
所以你们boss直接说join不要用于大数据。
而分别查的原因:比如我拿到对应这边的主键id,再到另外表去查,效率肯定是高得不要不要的。
一句话:越简单的查询效率越高
真不明白数据量大的情况下join了会导致性能低下,如果数据量大了单独查询某个表就几百兆上G,加载到程序就花费好长时间
索引会加快查询速度
利用缓存(memchache等)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
