Saturday, 21 May 2016

php - Possible way to find Username from 1000,000 Users entries



I have Db of 100,000 users in MYSQL. In that DB i am having column ID,username,Fname,Lname, etc..
When www.example.com/Jim or www.example.com/123 (Where JIM is username and 123 is ID in the users table)




I am using MYSQL query : select * from users where ID = 123 OR username = Jim
I am executing above query in PHP.
Output of the above query is :



| ID | Username | fname  | lname   |
+----+----------+--------+---------+
|123 | jim | Jim | Jonson |


My Problem is its taking huge time to select username or ID in the DB.




I have used following query



SELECT * FROMusersUSE INDEX (UsersIndexId) where id=123



Is this right way to call Index ?



EXPLAIN SELECT * FROM  `users` WHERE ID =327



OP



id  select_type  table    type    possible_keys        key      key_len ref rows Extra 
1 SIMPLE users Const PRIMARY,UsersIndexId PRIMARY 4 const


1


Answer



I sugest you take a look at this: How MySQL Uses Indexes




Quoting from the first paragraph:




Indexes are used to find rows with specific column values quickly.
Without an index, MySQL must begin with the first row and then read
through the entire table to find the relevant rows. The larger the
table, the more this costs. If the table has an index for the columns
in question, MySQL can quickly determine the position to seek to in
the middle of the data file without having to look at all the data. If
a table has 1,000 rows, this is at least 100 times faster than reading

sequentially.




That should help speed up your search.



(Edit: Updated the link to a newer version of the SQL docs)



PS: More specifically, column indexes might be what you want.
You can find more info about adding indexes here: Create Index Syntax


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...