0

I have one table with 2 unique indexed fields named "username" and "emailid". when I have fire one query on this table with OR condition like

SELECT * FROM tableName where (username = 'aaaa' OR emailid = 'aaaa')

the explain statement shows me for above query is

"Using union(UX_EMailID,UX_UserName); Using where" 

under "SIMPLE" as select_type.

Is it ok for performance, or something is wrong. One more way to do this with me. first call with one field and if no record found then try for second field like as follows.

IF EXISTS (SELECT 1 FROM tableName where username = 'aaaa' ) THEN
   //do code
ELSE
    IF EXISTS (SELECT 1 FROM tableName where emailid = 'aaaa' ) THEN
       //do code
    ELSE
       //throw error 
    END IF; 
END IF; 

So, Now I have confusion, which is better way for performance.

EDIT :

table: usermst
~~~~~~~~~~~~~~
id        int         //primary key 
username  varchar(20) //unique index
emailid   varchar(50) //unique index

EXPLAIN:

id: 1, 
select_type: 'SIMPLE'
table: 'usermst'
type: 'index_merge' 
possible_keys: 'ux_username,ux_emailid'
key: 'ux_emailid,ux_username' 
key_len: '153,63' 
ref: null
rows: 2
Extra: 'Using union(ux_emailid,ux_username); Using where'
3
  • 2
    What's the full EXPLAIN output? And what's the table structure?
    – Ja͢ck
    Mar 11, 2014 at 8:42
  • 1
    It is unioning the 2 indexes, so should be OK for performance. You 2nd solution means doing 2 queries so would probably be slower. Another possible way of doing this would be SELECT * FROM tableName where username = 'aaaa' UNION SELECT * FROM tableName where emailid = 'aaaa' but I would only consider this if your orignal query was not unioning the indexes.
    – Kickstart
    Mar 11, 2014 at 9:13
  • @Jack, I have edited my question Mar 11, 2014 at 9:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy