Domanda

Please i need your help with how to include INDEXES in my tables, i've read about it in several tutorials but still cant implement it on my database . To increase the efficiency of my database: I have installed php_apc.dll on the wampserver and it's faster a bit, I have also tried to avoid the use of SELECT * in my queries where neccessary. But how to implement INDEX is my problem. I'll be glad if you can please point out where it'll be nesseccary to INDEX.

Here is my database schema

  Database: RESULTS
  -------------------

   Table: STUDENTS
   - studentID (int)
   - first_name (varchar)
   - last_name (varchar)
   - other_name (varchar)

   Table: COURSES
   - courseID (int)
   - course_code (varchar)
   - course_title (varchar)
   - course_unit (int)

   Table: SEMESTER
   - semesterID
   - semester_name

   Table: MAINTABLE
   - scoresID (int)
   - courseID (int)
   - studentID (int)
   - semester_name (varchar)
   - session (varchar) 
   - score (int)
   - grade (varchar)
   - remarks (varchar)

Most of my queries revolve round INSERT INTO MAINTABLE , STUDENTS ..... then SELECT FROM MAINTABLE , STUDENTS and COURSES

Thank you for your patience and time. I most appreciate it. Thanks.

**EXAMPLES OF QUERIES"

                   $query2 = mysql_query("SELECT       first_name, last_name
        FROM students 
      WHERE matric_no = '".$matric_no."' ");
    ($row2 = mysql_fetch_array($query2)); 

    $query3 = mysql_query("SELECT  SUM(c.        course_unit) AS 'TOTAL'
               FROM    maintable AS m  
           INNER JOIN students AS s ON
                m.matric_no = s.matric_no
          INNER JOIN courses AS c ON
             m.course_code = c.course_code
       WHERE m.matric_no = '".$matric_no."'
          AND m.level = '".$level."'") or 
 die (mysql_error());

             $query4 = mysql_query("SELECT  c.          course_unit, m.score 
         FROM    maintable AS m  
                 INNER JOIN students AS s ON
                      m.matric_no = s.matric_no
                  INNER JOIN courses AS c ON
           m.course_code = c.course_code
      WHERE m.matric_no = '".$matric_no."'
                      AND m.level = '".$level."'") 
      or die (mysql_error()); 

              $query5 = mysql_query("SELECT  c.         course_unit, m.score 
             FROM    maintable AS m  
         INNER JOIN students AS s ON
             m.matric_no = s.matric_no
                     INNER JOIN courses AS c ON
                 m.course_code = c.course_code
     WHERE m.matric_no = '".$matric_no."'")           or die (mysql_error()); 


    $query6 = mysql_query("SELECT  SUM(c.              course_unit) AS 'TOTAL'
                     FROM    maintable AS m  
                      INNER JOIN students AS s ON
                              m.matric_no = s.matric_no
                      INNER JOIN courses AS c ON
                 m.course_code = c.course_code
      WHERE m.matric_no = '".$matric_no."'")           or die (mysql_error());     



            $query7 = mysql_query("SELECT  m.                            course_code AS 'Course Code',        c.course_title AS 'Course Title'
 , c.course_unit AS 'Unit',
  m.score AS 'Score', m.grade AS 'Grade'
                      FROM    maintable AS m  
                      INNER JOIN students AS s ON
                m.matric_no = s.matric_no
                      INNER JOIN courses AS c ON
                 m.course_code = c.course_code
  WHERE m.matric_no = '".$matric_no."'
  AND m.level = '".$level."'") 
   or die (mysql_error());
               $number_cols = mysql_num_fields         ($query7);

            $query8 = mysql_query("SELECT  m.         score, m.course_code 
                      FROM    maintable AS m  
                      INNER JOIN students AS s ON
                              m.matric_no = s.matric_no
                      INNER JOIN courses AS c ON
                 m.course_code = c.course_code
         WHERE m.matric_no = '".$matric_no."'
      AND m.score >= 0 AND m.score < 40 ")         or die (mysql_error());

                   $query9 = mysql_query("SELECT          m.grade, m.course_code 
                      FROM    maintable AS m  
                      INNER JOIN students AS s ON
                              m.matric_no = s.matric_no
                      INNER JOIN courses AS c ON
               m.course_code = c.course_code
         WHERE m.matric_no = '".$matric_no."'
                      AND m.grade = 'AR'") 
    or die (mysql_error());

Please forgive my formating. I typed from a mobile. Thank you for your time.

È stato utile?

Soluzione

You should begin by running all queries with EXPLAIN. That should show you some pointers.

But if you are asking for a rule of thumb, this would be the general guideline:

  • index columns which are FOREIGN KEY in JOIN statements
  • index all the columns used in WHERE conditions
  • index all the columns used in ORDER BY

Also there seem to be a lot of VARCHAR searches. Some additional normalization might give beneficial results too. It is easier to search for level_id then for level name.

Also, as a side note: please stop using the ancient mysql_* function. They are 10+ years old, no longer maintained and the process of making them deprecated had already begun. Instead you should start using prepared statements with PDO or MySQLi. One should't write new code with mysql_* API.

Altri suggerimenti

This is a bit too general to be well answered here, but my guidance to you is to focus on your WHERE clauses - if you use something a lot in WHERE then put an index on it and it will make those queries faster. Also, if you do any joins, put indexes on what you use in your ON clauses. That's so over-generalizing that I expect to be pounced upon for saying it, but general questions beget general answers.

More specificity would be your showing your table structures and actually showing a query or two. But then it gets preciously close to our doing your work for you.

Hope this helps.

Another general answer is, if you search for something in columns which contains string data like students' first name or last name, I'd suggest you put an index on those columns.

This would increase speed of retrieving results.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top