Am fetching data from database i need to pass variable in my query.but am not getting how to pass variable in query import MySQLdb as mdb
con=mdb.connect(host="localhost",user="root", passwd="yogi@123", db="wisdomjo_db2011")
cur=con.cursor()
cur.execute("SELECT suggestion,related FROM `auto_related` WHERE suggestion!='' AND type='S' GROUP BY suggestion")
numrows = cur.rowcount
for x in xrange(0,numrows):
    row = cur.fetchone()
    cur.execute("SELECT suggestion,LOWER(REPLACE(suggestion,' ','')) as suggest,related FROM `auto_related` WHERE LOWER(REPLACE(suggestion,' ',''))=%s  AND type='S' ORDER BY Id LIMIT 0,6",(row[0]))
    data=cur.fetchall()
    print data
Do the JOIN in SQL, not with a client-side loop.  Cutting down on the number of round-trips to the server helps; cutting down on the number of queries to parse and execute helps, even though the resulting query is more complex.

But the real slowdown is not having the result of LOWER(REPLACE(suggestion,' ','')) as an indexed column in the table.