Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用索引列的MySQL更新查询_Mysql_Sql - Fatal编程技术网

使用索引列的MySQL更新查询

使用索引列的MySQL更新查询,mysql,sql,Mysql,Sql,我有一个表table1,列如下: id - int field1 - varchar(10) field2 - varchar(10) totalMarks - int 此外,我还使用列field1创建了一个索引 create index myindex1 on table1 (field1); 我需要使用field1或field2更新表项 UPDATE table1 SET totalMarks = 1000 WHERE field1='somevalue'; 或 哪个更新查询具有良好

我有一个表
table1
,列如下:

id - int
field1 - varchar(10)
field2 - varchar(10)
totalMarks - int 
此外,我还使用列
field1
创建了一个索引

create index myindex1 on table1 (field1);
我需要使用
field1
field2
更新表项

UPDATE table1 SET totalMarks = 1000 WHERE field1='somevalue';


哪个更新查询具有良好的性能?由于我们已经使用
field1
创建了一个索引,如果我们在where子句中使用
field1
,它会有好的性能吗?

一个简单的
update
语句,带有一个
where
子句,在索引列上具有相等比较,应该使用
where
子句的索引

这并不总是能提高性能,但在较大的表上通常能提高性能。在非常小的表中,数据都放在一个数据页上,引擎需要将索引和数据页都加载到内存中,这实际上比只在给定页上查找行要慢一点。这是一个边缘案例

我建议使用带有索引列的版本

UPDATE table1 SET totalMarks = 1000 WHERE field2='somevalue';