I have a complex query that return 5 rows. The complex query is a join of 3 tables and a table valued function. When I run the query without any order by, the query return in 2 seconds. When I run the query with a order by which references one of the columns from the table valued function, the select slows down to almost 3 minutes. If I run the query with a order by (but on any other column except those from the table valued function), the query runs fast as well. It seems liike the query bogs down when I do a order by on any column from the table valued function. Is there a bug in the sql server regards to this or am I missing something here? I tried putting the results of the join in a temp table and running a order by on the temp table (order by on a column from the table valued function) and that runs very fast. Any ideas?