When multiple users send this query to server it slows down

In summary, the goal of the query is to show only questions that are not yet answered. This can be done by simply giving a list of ALL (possible) exam questions. Then use array_rand or shuffle to get some random elements.
  • #1
chrisalviola
80
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
  • #2
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.
 
  • #3
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>";
 

Related to When multiple users send this query to server it slows down

1. Why does sending the same query to a server multiple times slow it down?

Sending the same query to a server multiple times can slow it down due to increased traffic and workload on the server. Each time a query is sent, the server must process and respond to it, which can lead to a build-up of requests and cause delays in response time.

2. How does the number of users affect the speed of a server when sending a query?

The more users that send the same query to a server, the slower it may become. This is because each user's request adds to the workload of the server, and if there are too many requests at once, the server may struggle to keep up with the demand and slow down as a result.

3. Can the type of query being sent affect the speed of a server?

Yes, the type of query being sent can have an impact on the speed of a server. Some queries may require more processing and resources from the server, which can slow it down compared to simpler queries.

4. Is there a way to prevent a server from slowing down when multiple users send the same query?

One way to prevent a server from slowing down when multiple users send the same query is to optimize the server's performance and resources. This can include implementing caching techniques, using load balancers, and allocating enough resources to handle the expected workload.

5. How can I determine if the slowdown is due to multiple users sending the same query?

To determine if the slowdown is caused by multiple users sending the same query, you can monitor the server's performance and resources. If there is a significant increase in traffic and processing time when the query is sent by multiple users, it is likely the cause of the slowdown.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Feedback and Announcements
Replies
0
Views
95K
  • Programming and Computer Science
Replies
4
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Computing and Technology
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Back
Top