表存在对no字段的索引
当执行
explain select * from tName where no='ZKL'时,结果为id  select_type  table  type   posssible_keys key         key_len ref rows Extra1      SIMPLE    tName  const   tname001      tname001      10         1执行
explain select count(*) from tName where no='ZKL',结果为
id  select_type  table  type   posssible_keys key         key_len ref rows Extra1      SIMPLE    tName  const   tname001      tname001      10         1   Using index两个疑问
1、为何第一句执行时,extra反而没任何提示?
2、用coun(*)不是会全表扫描么,怎么反而有提示using index?

解决方案 »

  1.   

    using index 并不代表是否全表扫描。它只是说明在INDEX中的数据就足够完成查询任务,不需要再去访问实现的表数据。
    这个COUNT(*), MYSQL只需要通过访问索引文件就可以了, 而SELECT *  还需要到数据文件中去取出相应的其它字段信息。
      

  2.   

    问题是我加了where条件了啊,如果只是单独count(*),那它只需要去读保存的记录行数即可。如果加where他会全扫描么?
      

  3.   

    不会,先从索引中找到符合条件的记录位置。 如果是SELCT* 则还要到数据文件中读取其它字段的信息。
      

  4.   

    所以当where条件是在索引上建立的,那么count(*)就不存在效率及所谓的全表扫描问题了?