# Insane query time (mySQL)

**URL:** https://forum.kirupa.com/t/insane-query-time-mysql/195622
**Category:** Uncategorized
**Created:** [August 1, 2006, 11:40pm UTC](https://forum.kirupa.com/t/insane-query-time-mysql/195622 "2006-08-01T23:40:23Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![StarXploser](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/starxploser/32/875_2.png) [@StarXploser](https://forum.kirupa.com/u/StarXploser)
#### Post date: [August 1, 2006, 11:40pm UTC](https://forum.kirupa.com/t/insane-query-time-mysql/195622/1 "2006-08-01T23:40:23Z")

</div>

Hello,  
I’ve noticed that one of my page has been EXTREMELY slow…  
I checked every function and i was able to determine where my problem is.

here is the function:

(Some stats: FRIENDS table has 30,000 entries and USERS table has 1000)

```php

function getNext( $n, $sort)
    {
        $id = $this->user->getID();
        $next = array();
        
        
        if($sort)
        {
            $result = mysql_query("SELECT id FROM users WHERE id NOT IN(SELECT id2 FROM friends WHERE id1=$id) AND id != $id ORDER BY packageclass DESC, dailyPoints DESC LIMIT $n");
        }
        else
            $result = mysql_query("SELECT id FROM users WHERE id NOT IN(SELECT id2 FROM friends WHERE id1=$id) AND id != $id ORDER BY RAND() LIMIT $n");
            
        if(!$result)
        {
            echo "error:".mysql_error();
        }
        else
        {
            while($row = mysql_fetch_array($result))
            {
                $next[] = $row['id'];
            }
        }
        return $next;
    }    

```

I’m guessing that this query is just killing the server.  
it takes about 30-60 seconds to finish.

What should i do ?
