The situation of using index in mysql: 1. If the in query complies with the leftmost principle, the index can be used normally; 2. If there is a range query in front of the query, then the joint index If it fails, the index will not be used; 3. When the query value type is int and the column type is vachar, the index will fail.
The operating environment of this tutorial: windows10 system, mysql5.7.17 version, Dell G3 computer.
Does in use indexes in mysql?
1. In mysql 5.7.17 version, in queries can use indexes
+-----------+ | version() | +-----------+ | 5.7.17 | +-----------+
2. The value type of the query is int, column The type is vachar, which will cause the index to fail.
3. The amount of data is too large (2 million) and there are many in conditions. This should be judged by the Mysql optimizer. When the amount of data is large, indexing for in queries may not be the best. (PS: This part must consider the judgment of MySQL's own optimizer. Regarding the execution principle of MySQL's optimizer, it is quite complicated. In short, it is the judgment of various conditions to select the optimal solution that MySQL thinks it is. )
So for the fields added to the joint index, will the in query be effective?
First of all: For joint indexes, we need to clarify a concept. For joint indexes, the execution order is matched from left to right, which is equivalent to order by id, name, etc. The first field must be in order. If you use the index of the second field, you must use the first field first, and you must ensure that the second field is in order.
Secondly: The leftmost prefix matching principle is a very important principle. MySQL will always match to the right until it encounters a range query (>, "3" and d = "4". If (a,b, Index in the order of c, d), d cannot use the index, because the c field performs a range query, and the joint index is invalid. If you create an index of (a, b, d, c), it can be used, a, b , the order of d can be adjusted arbitrarily.
, the result is the same as the above concept. If the in query is in compliance with the leftmost principle, the index can be used normally. But if the in query is preceded by Range query, then the joint index will be invalid, and naturally our in query will not use the index
Recommended learning: mysql video tutorial
The above is the detailed content of Does in use index in mysql?. For more information, please follow other related articles on the PHP Chinese website!

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use
