Php 这些查询后面会有索引吗
我正在制作一个customer表,其中需要索引中的4个字段。 这是表结构 id、用户名、状态、电子邮件、密码、全名、joinDate (id为自动递增和主键) 索引中的4个字段是: 用户名、状态、电子邮件、密码 我需要运行的查询有:Php 这些查询后面会有索引吗,php,mysql,indexing,Php,Mysql,Indexing,我正在制作一个customer表,其中需要索引中的4个字段。 这是表结构 id、用户名、状态、电子邮件、密码、全名、joinDate (id为自动递增和主键) 索引中的4个字段是: 用户名、状态、电子邮件、密码 我需要运行的查询有: select id from table where status='active' and email='emailId' and password='pass' limit 1 select id from table where userName='user
select id from table where status='active' and email='emailId' and password='pass' limit 1
select id from table where userName='user' and status='active' limit 1
select fullName from table where id='123' limit 1
我想知道所有这些查询是否都将遵循索引?
如果没有,我如何更改结构以便遵循索引?我正在使用mysql和php
谢谢我的猜测是“否”、“是”和“否”,按顺序排列。但是如果你有,猜测是愚蠢的
如何更改结构,以便遵循索引
这完全是倒退。数据库结构本质上是您的公共API。不要改变结构;更改索引
以下是您的查询和索引,它们可能工作得很好。(你不局限于一个索引,但不要相信“可能工作得很好”;用解释来衡量。)
这个查询(上面)可能不会使用现有的索引,因为WHERE子句中没有使用第一列userName
select id
from table
where userName='user'
and status='active' limit 1;
只要已按实际顺序列出索引中的列,就应使用现有索引进行此查询。(WHERE子句中的两列与索引中的前两列匹配。)使用EXPLAIN进行验证
select id
from table
where status='active'
and email='emailId'
and password='pass' limit 1;
create index on table (status, email, password);
select fullName
from table
where id='123' limit 1;
列“id”是主键;它已经有了一个索引
看起来您只是“需要”在{status,email,password}上添加一个索引
是的,你的索引很好。我喜欢使用php验证一些字段,因为它只是一个字段查询。但是您应该考虑将状态更改为数字TyyIn(1)或char(1),并使用1=Active,2=不活动,3=禁用,4=BAN < /P>
select id,status,password from table where email='emailId' limit 1
$db_rows = mysql_num_rows($result); // $db->num_rows;
if($row['status']=='active' && $row['password']==$pass && $db_rows==1){do stuff}
select id,status from table where userName='user' limit 1
if($row['status']=='active' && $db_rows==1){do stuff}
select fullName from table where id='123' limit 1
“遵循索引”是什么意思?如果要确定已排序的数据,需要在查询中指定排序顺序:
select*from table order by userName asc
,例如。很容易自己找到:使用EXPLAIN(),我的意思是如果在所有3个查询中都使用索引。索引从左到右,在第一个查询中,我跳过了索引中的第一列(用户名),谢谢!我喜欢这个主意。保留当前索引,并在userName上创建另一个单列索引,我认为这是可行的。