<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeremy Massel&#039;s Blog &#187; database</title>
	<atom:link href="http://masseltech.com/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://masseltech.com</link>
	<description>Because sometimes I have good ideas too</description>
	<lastBuildDate>Wed, 11 Aug 2010 17:16:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Select DataSet and number of total rows with one stored procedure</title>
		<link>http://masseltech.com/2010/08/select-dataset-and-number-of-total-rows-with-one-stored-procedure/</link>
		<comments>http://masseltech.com/2010/08/select-dataset-and-number-of-total-rows-with-one-stored-procedure/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 17:16:18 +0000</pubDate>
		<dc:creator>jeremy.massel</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[iPhone programming]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[useful]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://masseltech.com/?p=213</guid>
		<description><![CDATA[when you want to write a search using .net and MSSQL, it&#8217;s a pain. This is because you&#8217;re forced to select every row in the table and then only display a small subset of it. This works okay for tables that have a few hundred rows, as query caching can make this faster. But what [...]]]></description>
			<content:encoded><![CDATA[<p>when you want to write a search using .net and MSSQL, it&#8217;s a pain. This is because you&#8217;re forced to select every row in the table and then only display a small subset of it. This works okay for tables that have a few hundred rows, as query caching can make this faster. But what happens when you&#8217;re searching a table with half a million rows? </p>
<p>Unless you&#8217;re a complete masochist, you&#8217;re doing to want to split this into a more manageable data set, otherwise you&#8217;re gonna eat all the memory on your server. But this means that you can no longer use the DataSet.Tables[0].Rows.Count property to figure out how many rows you have. You can write a second stored procedure that&#8217;ll count the rows. But who wants to clog up their database with tons of stored procedures for no reason? Let&#8217;s consolidate it into one.</p>
<p>So what does this look like?</p>
<p>First: the stored procedure.</p>
<p>We&#8217;ll use output parameters to pass the row count back to our code</p>
<p><code><br />
create procedure [dbo].[Search]<br />
@searchText varchar(512), @recordsToReturn INT, @pageNumber INT, @numberofrows INT OUTPUT<br />
AS</p>
<p>-- get the page we want to view<br />
select * from<br />
(<br />
     select *,  ROW_NUMBER() OVER (ORDER BY creation_timestamp DESC) AS row from [table] where [table].columnName like '%' + @searchText + '%'<br />
)<br />
AS results WHERE row between (@pageNumber - 1) * @recordsToReturn + 1 and @pageNumber*@recordsToReturn;</p>
<p>-- get the total number of rows, not just the subset we want<br />
set @numberofrows = (select count(*)  from [table] where [table].columnName like '%' + @searchText + '%')</p>
<p>END</p>
<p></code></p>
<p>Now the C# (this&#8217;ll work in VB too, but feel free to convert it yourself)</p>
<p><code><br />
      SqlConnection conn = new SqlConnection();<br />
      conn.ConnectionString = ".....your connection string here.....";<br />
      conn.Open();</p>
<p>      DataSet returnData = new DataSet();</p>
<p>      SqlDataAdapter da = new SqlDataAdapter( "SearchMessages", conn);<br />
      da.SelectCommand.CommandType = CommandType.StoredProcedure;</p>
<p>      da.SelectCommand.Parameters.Add("@searchText", SqlDbType.VarChar).Value = "bob";<br />
      da.SelectCommand.Parameters.Add("@recordsToReturn", SqlDbType.Int).Value = 10;<br />
      da.SelectCommand.Parameters.Add("@pageNumber", SqlDbType.Int).Value = 1;</p>
<p>      //number of rows<br />
      SqlParameter outputParameter = new SqlParameter("@numberofrows", SqlDbType.Int, 2);<br />
      outputParameter.Direction = ParameterDirection.Output;  </p>
<p>      da.SelectCommand.Parameters.Add(outputParameter);</p>
<p>      da.Fill(returnData, "theData");</p>
<p>      int numberOfRowsInDataSet = (int)outputParameter.Value;</p>
<p>      da.Dispose();<br />
      conn.Close();<br />
</code></p>
<p>Best of luck! As always, leave a message in the comments if you have questions</p>
]]></content:encoded>
			<wfw:commentRss>http://masseltech.com/2010/08/select-dataset-and-number-of-total-rows-with-one-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Batch Delete Performance SQL Server</title>
		<link>http://masseltech.com/2010/06/batch-delete-performance-sql-server/</link>
		<comments>http://masseltech.com/2010/06/batch-delete-performance-sql-server/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 17:38:32 +0000</pubDate>
		<dc:creator>jeremy.massel</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://masseltech.com/?p=187</guid>
		<description><![CDATA[Deleting old records from a table with > 3 000 00 rows. What&#8217;s the best way to do this? It seems the fastest way to do this is simply to: delete from [table] where creation_timestamp < dateadd (mm, -6, getdate()) (deleting anything older than 6 months) It took 3 hours (10916 seconds to be exact) [...]]]></description>
			<content:encoded><![CDATA[<p>Deleting old records from a table with > 3 000 00 rows. What&#8217;s the best way to do this? </p>
<p>It seems the fastest way to do this is simply to:<br />
<code><br />
delete from [table] where creation_timestamp < dateadd (mm, -6, getdate())<br />
</code><br />
(deleting anything older than 6 months)</p>
<p>It took 3 hours (10916 seconds to be exact) to delete 1.6 million (1,619,433) records this way. (148.35 / second).</p>
<p>We needed to do a second batch the next day, but wanted to split it into batches to try to get better performance.</p>
<p>Running: </p>
<p><code><br />
delete from [table] where pk_id in(<br />
select pk_id from (<br />
SELECT ROW_NUMBER() OVER (ORDER BY creation_timestamp desc) AS RowNumber, pk_id<br />
FROM [table]<br />
where creation_timestamp < '2009-12-16 13:52:08.673') _objectsToDelete<br />
WHERE RowNumber between 1 and 100000)<br />
</code></p>
<p>takes 12 minutes. (732 seconds) (136.61 / second).</p>
<p>Strangely, using the TOP command with a subquery takes the longest:</p>
<p><code><br />
delete from [table] where pk_id in (select TOP 100000 pk_id from [table] where creation_timestamp < '2009-12-16 13:52:08.673')<br />
</code></p>
<p>15 minutes (904 seconds) (110.61 / second)</p>
<p>Have a better way? Let me know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://masseltech.com/2010/06/batch-delete-performance-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql command doesn&#8217;t work in OS X snow leopard</title>
		<link>http://masseltech.com/2010/01/mysql-command-doesnt-work-in-os-x-snow-leopard/</link>
		<comments>http://masseltech.com/2010/01/mysql-command-doesnt-work-in-os-x-snow-leopard/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 20:09:01 +0000</pubDate>
		<dc:creator>jeremy.massel</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Web Hosting]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mySql]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://masseltech.com/?p=155</guid>
		<description><![CDATA[So you install mySql on your mac, and it&#8217;s working great, but then you go into terminal to run a mysqldump or something, and it doesn&#8217;t work! you get a message something like: -bash: mysqldump: command not found so how do you fix this? well, copy and paste the following into your terminal window and [...]]]></description>
			<content:encoded><![CDATA[<p>So you install mySql on your mac, and it&#8217;s working great, but then you go into terminal to run a mysqldump or something, and it doesn&#8217;t work!</p>
<p>you get a message something like:<br />
<code><br />
-bash: mysqldump: command not found<br />
</code></p>
<p>so how do you fix this? well, copy and paste the following into your terminal window and press enter:<br />
<code><br />
echo export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" >> ~/.profile</p>
<p>source ~/.profile<br />
</code></p>
<p>whoa, whoa, you say. what is this doing? It makes it so that when you type any command into the terminal, it does a check of these folders before giving you the &#8220;command not found&#8221; error. </p>
<p>If you want to undo it, you can always edit your .profile, or just delete it. </p>
]]></content:encoded>
			<wfw:commentRss>http://masseltech.com/2010/01/mysql-command-doesnt-work-in-os-x-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
