what is the difference between this and eq_ref?
what is the difference between this and eq_ref?
in a nutshell, const
is read directly by pressing the primary key or unique key, and eq_ref
is used in the case of join table query, by pressing the primary key or unique key of the join table.
the following is translated from official file :
const
the table has at most one matching row, which is read at the beginning of the query. Because there is only one row, the values of the columns in that row can be treated as constants by the rest of the optimizer. Const tables are very fast because they are read only once.
const is used to compare all parts of the Primary key or unique index with constant values. In the following query, tbl_name can be used as the const table:
SELECT * FROM tbl_name WHERE primary_key=1;
SELECT * FROM tbl_name
WHERE primary_key_part1=1 AND primary_key_part2=2;
eq_ref
reads a row composed of each row in this table and in the associated table. This is the best join type except for the system and const types. This value is used when the join uses all parts of the index and the index is a primary key or the only non-NULL index.
eq_ref can be used for index columns that are compared using the =
operator. The comparison value can be a constant or an expression of a column in a table that was read before using this table. In the following example, MySQL can use eq_ref connection (join) ref_table to handle:
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;
SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;
all of the following parse the meaning of each attribute of explain in detail:
:
id:
select_type:
SIMPLE
:UNION
PRIMARY
SELECT
WHERE
:SUBQUERY
table:
type:
from left to right, performance changes from poor to good
difference between eq_ref and const:
)
eq_ref: unique index scan, with only one record in the table matching for each index key. Common for primary key or unique index scanning
const, system: has at most one matching row in a single table, which can be queried very quickly, such as by primary key or unique index. System is a special case of the const type, using system when the query table has only one row.
select 1 from test where 1
.
possible_keys: indicates the index that may be used in the query. If it is empty, there is no relevant index. To improve performance at this point, check the WHERE clause to see if some fields are referenced, or check that the fields are not suitable for the index
key: shows the index that MySQL actually decides to use. If no index is selected, it is NULL
key_len: use the length of the index field
Note: the value displayed by key_len is the maximum possible length of the index field, not the actual length used, that is, the key_len is calculated based on the table definition, not retrieved within the table.
ref: shows which field or constant is used with key
rows: this number indicates how much data mysql has to traverse before it can be found. It means that MySQL estimates the number of rows needed to find the required records based on table statistics and index selection, which may be inaccurate on innodb
.Extra: description and description of the execution. Contains additional information that is not suitable for display in other columns but is very important.
Previous: BootstraoTable dynamic editing cell
Next: How to determine the date element in python requests.post?
$selectTip = $pdo->query("SELECT * FROM `tips_rate` WHERE `tip_id` = ".$row[ id ]." " ); $selecttotal = mysqli_num_rows($selectTip); $rate = $pdo->query("SELEC...
for example, the isolation level of transaction An is repeatable read my understanding is that a read sql statement of transaction An is the same as the result repeated many times, regardless of whether or not other transactions have changed the data ...
I know that mysql master and slave have been configured, but I don t know what to do if one of them dies: 1. If there is only one master and one slave, do you want to upgrade the master manually in case the master dies? (it seems that you can t call...
subject table, which can be understood as a classification table. The course and source_book tables are associated with subject now use the subject table as the main table to query how many courses and books are under this category. query statemen...
do a check-in function `create_time` decimal(20,3) NOT NULL, `create_date_ymd` varchar(15) NOT NULL COMMENT , `create_date_ym` varchar(15) NOT NULL, `create_date_y` int(4) NOT NULL, `create_date_m` int(2) NOT NULL, `create_date_d` int(2) ...
as shown in the following figure the current time is 16 41 00. I want to take this section of data every two hours. How should sql write ? ...
1. Problem description assume that there are n game order tables in the database, and the table name is game_ [0-9a-z] + _ order. . It is known that there is an order number 18demo000001. How can we efficiently find out which game table this order is i...
I want to optimize a sentence about group by. I don t know where to start. I want to find a train of thought . SELECT adv_id, network_id, SUM(re_click) AS clk, SUM(deny_click) AS dny_clk, SUM(re_conver) AS re_ins, SUM(mat_conver) AS mat_ins, SUM(def_...
background: 1. I am a java programmer 2. Recently interviewed for a senior development position 3. The previous project mainly used less oracle,mysql, and the companies interviewed basically used mysql s 4. I am weak in the database 5. Recently, I...
like this SELECT * FROM material WHERE {key:value} limit 1 ...
how does sql write from the database table to the display of the page? requirement: the first-level classification needs sorting, the first-level classification shows its subcategories, and the subcategories also need sorting. ...
check-in table: fields include user id userid check-in time signtime timestamp type int check-in type type is divided into 0 and 1 for check-in and check-out, respectively. A user can sign twice a day, once check-in and one check-out. Check-in and ch...
Table structure ...
use mybatis to configure custom secondary cache. Using xml annotation can use < cache type= "com.test.dao.MybatisRedisCache " > to successfully use custom cache, but using @ CacheNamespace (implementation = MybatisRedisCache.class) on the dao i...
class essay extends Model { public function comment() { return $this->hasMany( App comment ) } } class comment extends Model { public function essay() { return $this->belongsTo( App essay ) } } ...
mybatis+mysql for java projects int batchAmount = 20000; for (int startIndex = 0; startIndex < size; startIndex += batchAmount) { int endIndex = startIndex + batchAmount; endIndex = (endIndex &...
Master and Slave built by mysql5.6, the newly created table is not backed up to the slave library master and slvae data can both correspond to can t the test library be automatically backed up to the slave library when establishing a master-slav...
I want to do a circular sequence to output pictures. Don t know how to write this code? Table structure for example, now the field ID of key=1 is 1, 2, 5, 7, 10, 11, 6 fields I want to output one at a time in order. output id 1 field for t...
for example, if the database has a field type of enum ( axiomagementific database ), can you change this field to enum when there are millions of data in the database? will it be slow? ...
ask God to help me translate this native SQL statement into TP5 thank you! It is Db:: ( name )-> this kind of chained operation select a.id, a.titlerect a. Createwriting time as answer_count from fa_question a. Isometric solvepaper count (b.qid) as ...