Ik beheer een simpele interne website (voor opzoekingen van gebruikers / installaties / e.d.). In mijn mysql db zitten ongeveer 20 000 users en 30 000 studenten.
Om users of studenten op te zoeken op naam werkt dit zeer vlot via UNION. Helaas zijn er enkele users die meerdere telefoonnummers en werklocaties hebben (studenten hebben geen telefoonnummer en geen locatie).
De users, studenten, telefoonnummers en locaties steken allemaal in een andere tabel, dit is historisch zo gegroeid (aanpassingen zijn mogelijk).
Voor een opzoeking maak ik gebruik van UNION met enkele NULL waarden op de velden gelijk te houden. Van zodra het een user betreft ga ik, aan de hand van de guid, opzoeken welke telefoonnummers en locaties hij/zij heeft. Deze slaag ik tijdelijk in een variabele die ik na iedere gevonden gebruiker weergeef.
Dit zorgt voor de meeste vertraging (tot 2 seconden voor 10 users).
Dit script wordt aangeroepen via jquery en zodra er 3 letters in het inputfield staan. Bedoeling is ook dat je kan zoeken op locatie en bv. voornaam, of telefoonnummer en naam/locatie.
Hoe kan ik dit het beste optimaliseren? Zelf heb ik slechts een basiskennis van SQL en PHP.
Hieronder kan je een klein voorbeeld terugvinden van de opbouw van de database en het php script dat de opzoekingen doet.
tabel users
guid first_name last_name full_name account_name campus ou
abc a bc a bc abc S employees
def d ef d ef def S employees
ghi g hi g hi ghi M employees
tabel telephone
guid telephone type active
abc +32123 A 1
abc +32456 S 1
def +32789 S 1
def +32123 S 1
ghi +32999 A 0
tabel location
guid location active
abc 123 1
abc 234 1
abc 456 0
def 123 1
ghi 876 1
tabel students
guid first_name last_name full_name account_name campus ou
s001 s 001 s 001 s001 S students
s002 s 002 s 002 s002 M students
s003 s 003 s 003 s003 D students
[CODE]
<?php
$name = $_POST['searchitem'];
$sql_add = '';
$sql_add_loc = '';
$key= explode(" ", $name);
for($i = 0; $i < count($key); $i++){
$sql_add .= "AND (first_name LIKE '%". $key[$i] . "%' OR last_name LIKE '%". $key[$i] . "%' OR full_name LIKE '%". $key[$i]. "%' OR account_name LIKE '%". $key[$i]. "%')";
$sql_add_loc .= "UNION (SELECT null, guid, location, null FROM location WHERE location LIKE '%". $key[$i] ."%')";
$sql_add_loc .= "UNION (SELECT null, guid, null, number FROM telephone WHERE number LIKE '%". $key[$i] ."%')";
}
$sql_emp = "(SELECT ou, guid, null, null FROM users WHERE active <> '2'";
$sql_stu = "(SELECT ou, guid, null, null FROM students WHERE active <> '2'";
$sql_limit = " LIMIT 25 ";
$sql = $sql_emp . $sql_add . ') UNION '. $sql_stu . $sql_add . ') '. $sql_add_loc . $sql_limit;
$result = mysqli_query($db, $sql);
$x = 0;
if (mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)){
$x++;
$locations = '';
$nummers = '';
if ($row['ou'] <> 'students') {
$zoek_gegevens = "SELECT full_name, first_name, last_name, account_name, ou, campus FROM users WHERE guid='" . $row['guid']. "' LIMIT 1";
$zg = mysqli_query($db,$zoek_gegevens);
$gegevens = mysqli_fetch_assoc($zg);
$zoek_locatie = "SELECT location FROM location WHERE guid='" .$row['guid']."'";
$result_zoek_locatie = mysqli_query($db, $zoek_locatie);
if (mysqli_num_rows($result_zoek_locatie) > 0){
while ($locatie = mysqli_fetch_assoc($result_zoek_locatie)){
if ($locations == '') $locations = $locatie['location']; else $locations = $locations . ' <br> '. $locatie['location'];
}
}
else {
$locations = $gegevens['campus'];
}
$zoek_telefoon = "SELECT number FROM telephone WHERE guid='" .$row['guid']."'";
$result_zoek_telefoon = mysqli_query($db, $zoek_telefoon);
if (mysqli_num_rows($result_zoek_telefoon) > 0){
while ($nummer = mysqli_fetch_assoc($result_zoek_telefoon)){
if ($nummers == '') $nummers = $nummer['number']; else $nummers = $nummers . ' </small><br> <small> ' . $nummer['number'];
}
}
else {
$nummer = 'Unknown';
}
} else
{
$zoek_student = "SELECT full_name, first_name, last_name, account_name, ou, campus FROM students WHERE guid='" . $row['guid']. "' LIMIT 1";
$zs = mysqli_query($db,$zoek_student);
$gegevens = mysqli_fetch_assoc($zs);
$locations = $gegevens['campus'];
?>
2.519 views