如何提高数据库性能,减少数据库服务器压力瓶颈一两个小方法
树立的观念:一个SQL语句,如果在压力测试时CPU占有率达到100%,此语句肯定存在问题。 在大多数程序员的心里,一直认为数据库加索引,在查询中使用索引,就可以提高数据库性能。其实这种观点有一点的局限性,如果不好好的利用已经建立好的数据库索引,有时反
树立的观念:一个SQL语句,如果在压力测试时CPU占有率达到100%,此语句肯定存在问题。
在大多数程序员的心里,一直认为数据库加索引,在查询中使用索引,就可以提高数据库性能。其实这种观点有一点的局限性,如果不好好的利用已经建立好的数据库索引,有时反倒给数据库增加更大的处理压力。现在我们举例说明一下:
在现有的个人信息管理系统登录处的查询中,有这么一个SQL语句:
Select StudentInfoID, StudentNumber, StudentName,UnifyExamInfoID,NetSchoolID from StudentInfo
where StudentCrtificateNumber ='130425831217494' and rtrim(ltrim(StudentNumber)) = 'D0462382001'
此SQL语句的目的是查找这个考生是否存在,验证登录的效果,就是这么一个简单的SQL语句,在压力测试时,10个用户同时登录,就会造成数据库服务器 (2CPU,4G内存,IBM专用服务器)CPU占有率达到100%。而这张表也建立了StudentCrtificateNumber 和 StudentNumber 两个字段的索引, 问题出在哪呢?原因如下:
(1)在studentinfo表中,建立了一个索引LX_STUDENTINFO,包括了四个字段,顺序为:studycenternumber,studentnumber,studentname,studentcrtificatenumber。
(2)程序员理解的是,这个索引中包括了这两个字段,那么查询时就应该很快。实际不是。
大家注意一点,这个LX_STUDENTINFO索引,是四个索引字段,而他们的顺序 studycenternumber,studentnumber,studentname,studentcrtificatenumber,在数据库 设计中,如果查询采用的第一个字段和第二个字段组合,则该索引有效;如果查询采用第一个、第二个和第三个字段组合,该索引也有效;而如果第一个和第三个, 或第四个组合,则该索引是无效的,等同的效果仍是在没有索引的数据库中进行查询。也就是说建立的索引中的字段,在查询使用时,不能隔字段使用。
另外,索引中涉及到字段的顺序也会影响查询的性能,如果一个索引包含两个字段A1和A2,那么在查询时,一定要严格按索引中这两个字段的顺序设计查询,比 如:select * from a where A1='A' and A2='B'。而如果程序员写成了select * from a where A2='B' and A1='A'的话,虽然不影响程序正常使用,但会增加数据库的压力,并且查询效能会降低。
根据这种思路,重新调整了一下这个表的索引,在这个表中增加了一个新的索引,只包括这两个字段,并且按StudentCrtificateNumber 和StudentNumber先后顺序排序。经过300人同时登录的压力测试,CPU占有率才只占2-3%,证明调整结果有效。

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

AI Hentai Generator
Generate AI Hentai for free.

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

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

According to news from this website on July 23, ASUS has launched a variety of server and workstation-level products powered by AMD EPYC 4004 series processors. Note from this site: AMD launched the AM5 platform and Zen4 architecture EPYC 4004 series processors in May, offering up to 16-core 3DV-Cache specifications. ASUSProER100AB6 server ASUSProER100AB6 is a 1U rack server product equipped with EPYC Xiaolong 4004 series processor, suitable for the needs of IDC and small and medium-sized enterprises. ASUSExpertCenterProET500AB6 workstation ASUSExpertCenterProET500AB6 is a

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.
