Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the Fastest Way to Select a Whole Table in SQL Server?
    primarykey
    data
    text
    <p>I am writing a app that reads a whole table, does some processing, then writes the resulting data to another table. I am using the <code>SqlBulkCopy</code> class (.net version of "bcp in") which does the insert very fast. But I cannot find any efficent way to select data in the first place. there is not .net equivilent of "bcp out", which seems strange to me.</p> <p>Currently I'm using <code>select * from table_name</code>. For prespective it takes 2.5 seconds to select 6,000 rows ... and only 600ms to bulk insert the same number of rows.</p> <p>I would expect that selecting data should always be faster than inserting. <strong>What is the fastest way to select all rows &amp; columns from a table?</strong></p> <hr> <p>Answers to qeustions:</p> <ul> <li>I timed my select to take 2.5 seconds 2 ways. First was while running my application and running a sql trace. second was running the same query in SSMS. Both retured about the same result.</li> <li>I am reading data using SqlDataReader. </li> <li>No other applications are using this database.</li> <li>My current processing takes under 1 second, so 2+ second read time is relatively large. But mostly I'm concerned(interested) in performance when scaling this up to 100,000 rows and millions of rows.</li> <li>Sql Server 08r2 and my application are both running on my dev machine.</li> <li>Some of the data processing is set based so I need to have the whole table in memory (to support much larger data sets, I know this step will probably need to be moved into SQL so I only need to operate per row in memory)</li> </ul> <p>Here is my code:</p> <pre><code>DataTable staging = new DataTable(); using (SqlConnection dwConn = (SqlConnection)SqlConnectionManager.Instance.GetDefaultConnection()) { dwConn.Open(); SqlCommand cmd = dwConn.CreateCommand(); cmd.CommandText = "select * from staging_table"; SqlDataReader reader = cmd.ExecuteReader(); staging.Load(reader); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload