<p>If you're creating a temporary table within a stored procedure and want to add an index or two on it, to improve the performance of any additional statements made against it, what is the best approach? Sybase says <a href="http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc20020_1251/html/databases/databases644.htm" rel="noreferrer">this</a>:</p>

<p><em>"the table must contain data when the index is created. If you create the temporary table and create the index on an empty table, Adaptive Server does not create column statistics such as histograms and densities. If you insert data rows after creating the index, the optimizer has incomplete statistics."</em></p>

<p>but recently a colleague mentioned that if I create the temp table and indices in a different stored procedure to the one which actually uses the temporary table, then Adaptive Server optimiser <em>will</em> be able to make use of them.</p>

<p>On the whole, I'm not a big fan of wrapper procedures that add little value, so I've not actually got around to testing this, but I thought I'd put the question out there, to see if anyone had any other approaches or advice?</p>

[{'answer_id': 53814, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': False, 'text': "<p>What's the problem with adding the indexes after you put data into the temp table?</p>\n\n<p>One thing you need to be mindful of is the visibility of the index to other instances of the procedure that might be running at the same time.</p>\n\n<p>I like to add a guid to these kinds of temp tables (and to the indexes), to make sure there is never a conflict.  The other benefit of this approach is that you could simply make the temp table a real table.</p>\n\n<p>Also, make sure that you will need to query the data in these temp tables <em>more than once</em> during the running of the stored procedure, otherwise the cost of index creation will outweigh the benefit to the select.</p>\n"}, {'answer_id': 66620, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>In Sybase if you create a temp table and then use it in one proc the plan for the select is built using an estimate of 100 rows in the table. (The plan is built when the procedure starts before the tables are populated.) This can result in the temp table being table scanned since it is only "100 rows". Calling a another proc causes Sybase to build the plan for the select with the actual number of rows, this allows the optimizer to pick a better index to use.  I have seen significant improvedments using this approach but test on your database as sometimes there is no difference. </p>\n'}, {'answer_id': 153680, 'author': 'AJ.', 'author_id': 7211, 'author_profile': 'https://Stackoverflow.com/users/7211', 'pm_score': 4, 'selected': True, 'text': '<p>A few thoughts: </p>\n\n<ul>\n<li>If your temporary table is so big that you have to index it, then is there a better way to solve the problem?  </li>\n<li><p>You can force it to use the index (if you are sure that the index is the correct way to access the table) by giving an optimiser hint, of the form: </p>\n\n<pre><code>SELECT * \nFROM   #table (index idIndex) \nWHERE  id = @id\n</code></pre></li>\n</ul>\n\n<p>If you are interested in performance tips in general, I\'ve answered a couple of other questions about that at some length here:  </p>\n\n<ul>\n<li><a href="https://stackoverflow.com/questions/18783/sql-what-are-your-favorite-performance-tricks#103176">Favourite performance tuning tricks</a></li>\n<li><a href="https://stackoverflow.com/questions/137226/how-do-you-optimize-tables-for-specific-queries#138949">How do you optimize tables for specific queries?</a></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/53734', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1030/']