When multiple users send this query to server it slows down

  • Thread starter Thread starter chrisalviola
  • Start date Start date
  • Tags Tags
    Multiple Server
AI Thread Summary
The discussion centers on optimizing a SQL query used in a PHP application to retrieve unanswered exam questions. The current query is complex and slows down when multiple users access it simultaneously. Suggestions include simplifying the query by removing redundant subqueries, particularly the part checking if `ans_exam_id` is in the list of `exam_id`s from the `ExamQuestions` table. Instead, it is recommended to fetch all possible exam questions and then use PHP functions like `array_rand` or `shuffle` to randomly select unanswered questions. The goal is to ensure that only questions not yet answered by the user are displayed in random order while improving the query's efficiency to reduce server load. The discussion also touches on the database structure, which includes tables for users, admins, exam questions, and student answers, indicating a need for efficient data retrieval methods.
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>";
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
15
Views
2K
Replies
3
Views
13K
Replies
4
Views
6K
Replies
3
Views
4K
Replies
3
Views
3K
Replies
4
Views
3K
Replies
1
Views
10K
Replies
5
Views
4K
Back
Top