When multiple users send this query to server it slows down

  • Thread starter Thread starter chrisalviola
  • Start date Start date
  • Tags Tags
    Multiple Server
Click For Summary
SUMMARY

The discussion centers on optimizing a SQL query in PHP that retrieves unanswered exam questions from the ExamQuestions table. The original query, which includes a subquery to filter based on user answers, is identified as inefficient, particularly under high user load. Suggestions include simplifying the query to retrieve all exam questions and using PHP functions like array_rand or shuffle to randomize the selection, thereby reducing server traffic and improving performance.

PREREQUISITES
  • Understanding of SQL query optimization techniques
  • Familiarity with PHP programming and MySQL database interactions
  • Knowledge of SQL JOIN operations and their impact on performance
  • Basic understanding of database table structures and relationships
NEXT STEPS
  • Learn about SQL query optimization strategies, focusing on subquery reduction
  • Explore PHP array functions for efficient data handling, such as array_rand and shuffle
  • Investigate the use of SQL JOINs to improve data retrieval performance
  • Study MySQL indexing techniques to enhance query execution speed
USEFUL FOR

Backend developers, database administrators, and anyone involved in optimizing SQL queries for performance in web applications.

chrisalviola
Messages
80
Reaction score
0
This is my SQL in PHP
SELECT * FROM ExamQuestions where exam_id not in(Select ans_exam_id from StudAns where ans_user_id=".$_COOKIE['cuserid']." and ans_exam_id in(SELECT exam_id FROM ExamQuestions)) ORDER BY RAND()

when multiple users send this query to server it slows down. is there any way i can simplify this? do i need to use join or create view to increase fetching speed?
 
Technology news on Phys.org
Well, the "ans_exam_id in(SELECT exam_id FROM ExamQuestions)" part seems a bit redundant to me. I don't know your table structure, but does this actually add anything useful?

One thing you could do, since you are using PHP anyway, is simplify the query to simply give a list of ALL (possible) exam questions. Then use array_rand or shuffle to get some random elements.
 
CompuChip said:
Well, the "ans_exam_id in(SELECT exam_id FROM ExamQuestions)" part seems a bit redundant to me. I don't know your table structure, but does this actually add anything useful?

One thing you could do, since you are using PHP anyway, is simplify the query to simply give a list of ALL (possible) exam questions. Then use array_rand or shuffle to get some random elements.

The goal of my query is to show only questions that are not yet answered and it should be in random order.

if you could suggest the best query that would reduce traffic i would be great full.

heres my table structure

PHP:
    $query = "CREATE TABLE users (user_id INT (4) NOT NULL AUTO_INCREMENT, user_lastname TEXT,user_firstname TEXT, user_email TEXT, user_subcode TEXT, user_score INT (4), user_date DATETIME, user_ip TEXT, PRIMARY KEY (user_id))";

    $result = mysql_query($query) or die("Query failed");    

    print "Table users is created !<br>";



    $query = "CREATE TABLE admin (admin_username TEXT, admin_password TEXT, admin_ip TEXT, admin_date DATETIME)";

    $result = mysql_query($query) or die("Query failed");     

    print "Table admin is created !<br>";

    

    $query = "INSERT INTO admin (admin_username, admin_password) VALUES ('$ad_login', '$ad_passc')";

    $result = mysql_query($query) or die("Query failed");       

    print "Administrator account created !<br>";



    $query = "CREATE TABLE ExamQuestions (exam_id INT (4) NOT NULL AUTO_INCREMENT, exam_q LONGTEXT,opt1 TEXT, opt2 TEXT, opt3 TEXT, opt4 TEXT, correct_ans INT (4), PRIMARY KEY (exam_id))";

    $result = mysql_query($query) or die("Query failed");    

    print "Table ExamQuestions is created !<br>";



    $query = "CREATE TABLE StudAns (ans_id INT (4) NOT NULL AUTO_INCREMENT, ans_user_id INT (4), ans_exam_id INT (4), stud_ans INT (4), PRIMARY KEY (ans_id))";

    $result = mysql_query($query) or die("Query failed");    

    print "Table StudAns is created !<br></div>";
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 5 ·
Replies
5
Views
5K