<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3561617962469253143</id><updated>2024-10-10T03:24:38.150-07:00</updated><category term="C#"/><category term="Linq"/><category term="Sql"/><title type='text'>Ryan&#39;s coding blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ryancoder.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://ryancoder.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ryan Hennig</name><uri>http://www.blogger.com/profile/00825532811928979346</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3561617962469253143.post-7851337935782482321</id><published>2016-01-19T11:35:00.001-08:00</published><updated>2016-01-19T11:40:34.027-08:00</updated><title type='text'>Connecting DataGrip to Redshift</title><content type='html'>&lt;h3 style=&quot;margin: 0px; position: relative;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; font-size: 13px; font-weight: normal; line-height: 18.2px;&quot;&gt;Recently I started evaluating JetBrains&#39; new&amp;nbsp;&lt;a href=&quot;https://www.jetbrains.com/datagrip/&quot;&gt;DataGrip&lt;/a&gt;&amp;nbsp;product, and I decided to create an updated version of my&amp;nbsp;&lt;a href=&quot;http://ryancoder.blogspot.com/2015/08/connecting-intellij-to-redshift.html&quot;&gt;instructions for connecting to Amazon Redshift&lt;/a&gt;, using this new tool with the latest JDBC driver from Amazon.&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;margin: 0px; position: relative;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;margin: 0px; position: relative;&quot;&gt;
Prepare Amazon Redshift JDBC Driver&lt;/h3&gt;
&lt;div&gt;
&lt;ol style=&quot;font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2px;&quot;&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Browse to your AWS Redshift console, go to the &quot;Connect Client&quot; section and download the Redshift JDBC driver from Amazon.&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Copy the file from your Downloads folder to somewhere more permanent, like your home directory or a location accessible to all users on the computer.&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Set permissions to allow everyone to read the driver jar file:&lt;pre&gt;chmod a+r RedshiftJDBC41-1.1.9.1009.jar&lt;/pre&gt;
&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;&lt;pre&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 18.2px; white-space: normal;&quot;&gt;On OS X, you may need to remove the quarantine mode attribute:&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;You can see this attribute with the &quot;ls -l@&quot; command:
&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;ls -l@ *.jar&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;-rw-r--r--@ 1 ryan staff 2198193 Aug 28 13:42 RedshiftJDBC41-1.1.9.1009.jar&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;&amp;nbsp;com.apple.metadata:kMDItemWhereFroms 194&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;&amp;nbsp;com.apple.quarantine 64
&lt;span style=&quot;line-height: 1.4;&quot;&gt;
The quarantine attribute can be removed with this command:&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , &amp;quot;tahoma&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;freesans&amp;quot; , sans-serif; line-height: 1.4; white-space: normal;&quot;&gt;xattr -dr com.apple.quarantine RedshiftJDBC*.jar&lt;/span&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2px;&quot;&gt;
&lt;h3 style=&quot;margin: 0px; position: relative;&quot;&gt;
Add Redshift Driver to DataGrip&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18.2px;&quot;&gt;
&lt;ol&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;In Datagrip, go to Manage Data Source (Cmd + ; on OS X)&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Press the + button in the upper left and select &quot;Driver&quot; to create a new Database Driver&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQ1TShiEFKRg4m80F6kqCxN7gci_davWVQKFgYkMNqUqmMWe60G9Jjza1VWR0WR-_SGFkx_QxdAup50BbavIhIfCRRPhhkHlxXxNuh_JbxNyajCNgRJWD61Al1lG5voKg8CIh9G_9z3I/s1600/DataGrip+Add+Driver.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;425&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQ1TShiEFKRg4m80F6kqCxN7gci_davWVQKFgYkMNqUqmMWe60G9Jjza1VWR0WR-_SGFkx_QxdAup50BbavIhIfCRRPhhkHlxXxNuh_JbxNyajCNgRJWD61Al1lG5voKg8CIh9G_9z3I/s640/DataGrip+Add+Driver.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;In the &quot;Driver Files&quot; section, press the + button in the lower left and select the path where you put the driver JAR file.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 18.2px;&quot;&gt;Use the following settings:&amp;nbsp;&lt;/span&gt;&lt;ul style=&quot;line-height: 1.4; list-style: disc; margin: 0.5em 0px; padding: 0px 2.5em;&quot;&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;Class Name: com.amazon.redshift.jdbc41.Driver&lt;/li&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;&lt;span style=&quot;line-height: 1.4;&quot;&gt;Dialect: PostgreSQL&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;&lt;span style=&quot;line-height: 1.4;&quot;&gt;Auto Commit:&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;&lt;span style=&quot;line-height: 1.4;&quot;&gt;Yes for convenience,&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;&lt;span style=&quot;line-height: 1.4;&quot;&gt;No for safety (This Requires you to explicitly commit after executing DML)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li style=&quot;border: none; margin: 0px 0px 0.25em; padding: 0.25em 0px;&quot;&gt;Auto Sync: Yes (turn this off if it slows you down too much)&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8rZGWtwTBAL0nmipNQaOFmsiipDEn6FVMQJJ66VSgTFT5Uw25tPkxglMgoElPOqRGWZinZKUozHIcfZ-JP5pm4LjdA1Ke4TJNr8z6COcKolPbRmlWdwqX2I0Bkidn9fXIaFbNk2bgePY/s1600/Screenshot+2016-01-14+13.17.24.png&quot; imageanchor=&quot;1&quot; style=&quot;line-height: 18.2px;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;285&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8rZGWtwTBAL0nmipNQaOFmsiipDEn6FVMQJJ66VSgTFT5Uw25tPkxglMgoElPOqRGWZinZKUozHIcfZ-JP5pm4LjdA1Ke4TJNr8z6COcKolPbRmlWdwqX2I0Bkidn9fXIaFbNk2bgePY/s400/Screenshot+2016-01-14+13.17.24.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;ul style=&quot;line-height: 1.4; list-style: disc; margin: 0.5em 0px; padding: 0px 2.5em;&quot;&gt;&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;line-height: 18.2px;&quot;&gt;Click Apply&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style=&quot;display: inline !important; line-height: 18.2px; margin: 0px; position: relative;&quot;&gt;
Connect to your Redshift Instance with the new Driver&lt;/h3&gt;
&lt;span style=&quot;line-height: 1.4;&quot;&gt;&lt;br style=&quot;line-height: 18.2px;&quot; /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;ol style=&quot;line-height: 18.2px;&quot;&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Press the + button again and select the name of the driver you just created (Redshift)&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Go to your Redshift Console in AWS. &amp;nbsp;Under &quot;Connect Client&quot;, select your cluster and get the JDBC URL.&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Copy the JDBC URL into the URL field in DataGrip&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Enter your username and password, and test the connection. &amp;nbsp;&lt;/li&gt;
&lt;li style=&quot;margin: 0px 0px 0.25em; padding: 0px;&quot;&gt;Hit OK. &amp;nbsp;You can now query Redshift from DataGrip!&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://ryancoder.blogspot.com/feeds/7851337935782482321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ryancoder.blogspot.com/2016/01/connecting-datagrip-to-redshift_19.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/7851337935782482321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/7851337935782482321'/><link rel='alternate' type='text/html' href='http://ryancoder.blogspot.com/2016/01/connecting-datagrip-to-redshift_19.html' title='Connecting DataGrip to Redshift'/><author><name>Ryan Hennig</name><uri>http://www.blogger.com/profile/00825532811928979346</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgQ1TShiEFKRg4m80F6kqCxN7gci_davWVQKFgYkMNqUqmMWe60G9Jjza1VWR0WR-_SGFkx_QxdAup50BbavIhIfCRRPhhkHlxXxNuh_JbxNyajCNgRJWD61Al1lG5voKg8CIh9G_9z3I/s72-c/DataGrip+Add+Driver.png" height="72" width="72"/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3561617962469253143.post-6815960923011124848</id><published>2015-08-31T12:51:00.000-07:00</published><updated>2015-08-31T12:52:20.339-07:00</updated><title type='text'>Connecting IntelliJ to Redshift</title><content type='html'>Are you using Amazon&#39;s Redshift Service and you want to connect to it via IntelliJ? &amp;nbsp;Here&#39;s how you do it.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Download Redshift JDBC Driver from Amazon&lt;/h3&gt;
&lt;div&gt;
&lt;ol&gt;
&lt;li&gt;In your Redshift console, go to the &quot;Connect Client&quot; section and download the Redshift JDBC driver from Amazon.&lt;/li&gt;
&lt;li&gt;Copy the file from your Downloads folder to somewhere more permanent, like your home directory or a location accessible to all users on the computer.&lt;/li&gt;
&lt;li&gt;Set permissions to allow everyone to read the jar file:&lt;br /&gt;
&lt;pre&gt;chmod a+r RedshiftJDBC41-1.1.7.1007.jar&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
Add Redshift Driver to IntelliJ&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ol&gt;
&lt;li&gt;In IntelliJ, go to View / Tool Windows / Database&lt;/li&gt;
&lt;li&gt;Open the Data Source Properties&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKbytGc8kQAnhCn_KFxiFaNC0cNabANulKYkDBImnc4QwwMsReUr0nBjO7uzptKAKTSJj7ssdeAPwLw_buv34BL9jMXu1RYLx96tBe_iokAa2bmPxw8QJoNz2dgobo1fV0-ggN7M4sN0I/s1600/IntelliJ+Data+Selection+Tool.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;83&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKbytGc8kQAnhCn_KFxiFaNC0cNabANulKYkDBImnc4QwwMsReUr0nBjO7uzptKAKTSJj7ssdeAPwLw_buv34BL9jMXu1RYLx96tBe_iokAa2bmPxw8QJoNz2dgobo1fV0-ggN7M4sN0I/s320/IntelliJ+Data+Selection+Tool.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Press the + button and select &quot;Database Driver&quot; to create a new Redshift Driver&lt;/li&gt;
&lt;li&gt;In the &quot;Driver Files&quot; section, press the + button and select the path where you put the driver JAR file.&lt;/li&gt;
&lt;ul&gt;
&lt;/ul&gt;
&lt;li&gt;If the driver can be read successfully, the Class Name drop down should show more than just &quot;java.sql.Driver&quot;. &amp;nbsp;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;In my case, it couldn&#39;t be read because OS X had put the file in quarantine mode, which you can see with the &quot;ls -l@&quot; command:
&lt;pre&gt;ls -l@ *.jar
-rw-r--r--@ 1 root  wheel  2198193 Aug 28 13:42 RedshiftJDBC41-1.1.7.1007.jar
 com.apple.metadata:kMDItemWhereFroms     211
 com.apple.quarantine      67&lt;/pre&gt;
&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;
The quarantine attribute can be removed with this command:&lt;br /&gt;
&lt;pre&gt;xattr -dr com.apple.quarantine RedshiftJDBC41-1.1.7.1007.jar&lt;/pre&gt;
&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Use the following settings: &lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi29f-yqWBPcbOOy5SxNZ6hqFwsRCwk2LpipVlC0kMMFmbjILD18yVxQp6MI1LBp5j_KqciklgSLnYFVeRRhsKf5IXBmtrhEMvhEjNt53RTTrSNVweyxa1aYQo10vOTOT0_qazrE7LSlaQ/s1600/Redshift+Driver+Settings.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi29f-yqWBPcbOOy5SxNZ6hqFwsRCwk2LpipVlC0kMMFmbjILD18yVxQp6MI1LBp5j_KqciklgSLnYFVeRRhsKf5IXBmtrhEMvhEjNt53RTTrSNVweyxa1aYQo10vOTOT0_qazrE7LSlaQ/s640/Redshift+Driver+Settings.png&quot; width=&quot;444&quot; /&gt;&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;Class Name: com.amazon.redshift.jdbc41.Driver&lt;/li&gt;
&lt;li&gt;URL Template:&amp;nbsp;jdbc:redshift://{host}:{port}/{database}&lt;/li&gt;
&lt;li&gt;Default Port: 5439 (or whatever your cluster uses)&lt;/li&gt;
&lt;li&gt;Dialect: PostgreSQL&lt;/li&gt;
&lt;li&gt;Requires Authentication: Yes&lt;/li&gt;
&lt;li&gt;Local Database: No&lt;/li&gt;
&lt;li&gt;Auto Commit: Yes for convenience, No for safety (This Requires you to explicitly commit after executing DML)&lt;/li&gt;
&lt;li&gt;Auto Sync: Yes (turn this off if it slows you down too much)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click Apply&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;h3&gt;
Connect to your Redshift Instance with the new Driver&lt;/h3&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;In the Same Data Sources and Drivers Window in IntelliJ, press the + button again and select Redshift&lt;/li&gt;
&lt;li&gt;Go to your Redshift Console in AWS. &amp;nbsp;Copy the string under &quot;Endpoint&quot; into the &quot;Host&quot; field in IntelliJ. &amp;nbsp;this is in host:port format, so cut the port off, delete the &quot;:&quot; and paste the port into the port field.&lt;/li&gt;
&lt;li&gt;Enter your database name, user, and password, and test the connection. &amp;nbsp;&lt;/li&gt;
&lt;li&gt;Hit OK. &amp;nbsp;You can now query Redshift from your IDE!&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://ryancoder.blogspot.com/feeds/6815960923011124848/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ryancoder.blogspot.com/2015/08/connecting-intellij-to-redshift.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/6815960923011124848'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/6815960923011124848'/><link rel='alternate' type='text/html' href='http://ryancoder.blogspot.com/2015/08/connecting-intellij-to-redshift.html' title='Connecting IntelliJ to Redshift'/><author><name>Ryan Hennig</name><uri>http://www.blogger.com/profile/00825532811928979346</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKbytGc8kQAnhCn_KFxiFaNC0cNabANulKYkDBImnc4QwwMsReUr0nBjO7uzptKAKTSJj7ssdeAPwLw_buv34BL9jMXu1RYLx96tBe_iokAa2bmPxw8QJoNz2dgobo1fV0-ggN7M4sN0I/s72-c/IntelliJ+Data+Selection+Tool.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3561617962469253143.post-3993144030179499670</id><published>2010-06-10T19:45:00.000-07:00</published><updated>2010-06-10T20:09:28.019-07:00</updated><title type='text'>Bucket DateTimes in T-SQL</title><content type='html'>Today at work I was working on a SQL Server Reporting Services report which shows us the number of rows being created in a certain table in each 10-minute interval.  While working on this, I tried several different ways in which I could &quot;bucket&quot; the datetime column into the same ten-minute period.  At the end, I finally decided to extract this logic into a User-Defined Function which can bucket any datetime into any size interval.&lt;br /&gt;&lt;br /&gt;Here&#39;s the function:&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;style type=&quot;text/css&quot;&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt; font-size: small;&lt;br /&gt; color: black;&lt;br /&gt; font-family: Consolas, &quot;Courier New&quot;, Courier, Monospace;&lt;br /&gt; background-color: #ffffff;&lt;br /&gt; /*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt; background-color: #f4f4f4;&lt;br /&gt; width: 100%;&lt;br /&gt; margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;/* BucketDateTime: &lt;br /&gt; * Buckets &lt;span class=&quot;kwrd&quot;&gt;any&lt;/span&gt; datetime &lt;span class=&quot;kwrd&quot;&gt;into&lt;/span&gt; an &lt;span class=&quot;kwrd&quot;&gt;interval&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;of&lt;/span&gt; the given &lt;span class=&quot;kwrd&quot;&gt;size&lt;/span&gt;. &lt;br /&gt; * &lt;span class=&quot;kwrd&quot;&gt;Returns&lt;/span&gt; the minimum datetime &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; the &lt;span class=&quot;kwrd&quot;&gt;interval&lt;/span&gt; chosen, &lt;span class=&quot;kwrd&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; @MinutesPerBucket &amp;lt; 1&lt;br /&gt; * Example: bucket times 7:29pm &lt;span class=&quot;kwrd&quot;&gt;and&lt;/span&gt; 7:31pm &lt;span class=&quot;kwrd&quot;&gt;to&lt;/span&gt; the nearest 30 &lt;span class=&quot;kwrd&quot;&gt;minute&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;interval&lt;/span&gt;&lt;br /&gt; * &lt;span class=&quot;kwrd&quot;&gt;SELECT&lt;/span&gt; dbo.BucketDateTime(&lt;span class=&quot;str&quot;&gt;&#39;2010-04-01 19:29:00&#39;&lt;/span&gt;, 30) =&amp;gt; &lt;span class=&quot;str&quot;&gt;&#39;2010-04-01 19:00:00.000&#39;&lt;/span&gt;&lt;br /&gt; * &lt;span class=&quot;kwrd&quot;&gt;SELECT&lt;/span&gt; dbo.BucketDateTime(&lt;span class=&quot;str&quot;&gt;&#39;2010-04-01 19:31:00&#39;&lt;/span&gt;, 30) =&amp;gt; &lt;span class=&quot;str&quot;&gt;&#39;2010-04-01 19:30:00.000&#39;&lt;/span&gt;&lt;br /&gt; */&lt;br /&gt; &lt;span class=&quot;kwrd&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;FUNCTION&lt;/span&gt; [dbo].[BucketDateTime]&lt;br /&gt;(&lt;br /&gt;    @SourceDateTime DATETIME, &lt;span class=&quot;rem&quot;&gt;-- The DateTime to bucket&lt;/span&gt;&lt;br /&gt;    @MinutesPerBucket &lt;span class=&quot;kwrd&quot;&gt;INT&lt;/span&gt;     &lt;span class=&quot;rem&quot;&gt;-- The number of minutes per bucket.&lt;/span&gt;&lt;br /&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;RETURNS&lt;/span&gt; DATETIME&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;IF&lt;/span&gt; (@MinutesPerBucket &amp;lt; 1) &lt;span class=&quot;kwrd&quot;&gt;RETURN&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;-- The earliest possible datetime&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;DECLARE&lt;/span&gt; @Epoch DATETIME                    &lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;-- # of minutes passed between the epoch and source datetime&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;DECLARE&lt;/span&gt; @MinutesSinceEpoch &lt;span class=&quot;kwrd&quot;&gt;INT&lt;/span&gt;            &lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;-- The value to bucket @SourceDateTime into, based on @MinutesPerBucket&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;DECLARE&lt;/span&gt; @BucketDateTime DATETIME        &lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;-- # of minutes passed between @BucketDateTime&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;DECLARE&lt;/span&gt; @BucketMinutesSinceEpoch &lt;span class=&quot;kwrd&quot;&gt;INT&lt;/span&gt;    &lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;-- This value is from Books online: http://msdn.microsoft.com/en-us/library/ms186724.aspx&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;SET&lt;/span&gt; @Epoch = &lt;span class=&quot;kwrd&quot;&gt;CAST&lt;/span&gt;(&lt;span class=&quot;str&quot;&gt;&#39;1753-01-01&#39;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;AS&lt;/span&gt; DATETIME)     &lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;SET&lt;/span&gt; @MinutesSinceEpoch = DATEDIFF(mi, @Epoch, @SourceDateTime)&lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;SET&lt;/span&gt; @BucketMinutesSinceEpoch = @MinutesSinceEpoch - (@MinutesSinceEpoch % @MinutesPerBucket)&lt;br /&gt;    &lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;SET&lt;/span&gt; @BucketDateTime = DATEADD(mi, @BucketMinutesSinceEpoch, @Epoch)&lt;br /&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;RETURN&lt;/span&gt; @BucketDateTime&lt;br /&gt;    &lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://ryancoder.blogspot.com/feeds/3993144030179499670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ryancoder.blogspot.com/2010/06/bucket-datetimes-in-t-sql.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/3993144030179499670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/3993144030179499670'/><link rel='alternate' type='text/html' href='http://ryancoder.blogspot.com/2010/06/bucket-datetimes-in-t-sql.html' title='Bucket DateTimes in T-SQL'/><author><name>Ryan Hennig</name><uri>http://www.blogger.com/profile/00825532811928979346</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3561617962469253143.post-3889711695362823083</id><published>2010-06-08T16:43:00.000-07:00</published><updated>2010-06-08T17:27:29.637-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Linq"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Execute LINQ queries without locking any tables</title><content type='html'>At work I&#39;m building an application which queries a SQL Server backend with LINQ to SQL, and I recently found that some of my queries were slow due to table locking.  I found &lt;a href=&quot;http://www.infoq.com/news/2008/03/linq-nolock&quot;&gt;this excellent blog post&lt;/a&gt; referencing &lt;a href=&quot;http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx&quot;&gt;Scott Hanselman&#39;s blog post&lt;/a&gt; which describes how to execute LINQ queries without locking the source tables:&lt;br /&gt;&lt;br /&gt;Deciding to go with option 1, I came up with an elegant solution for easily adding this to any LINQ queries.  Let&#39;s say you are using a LINQ query like this:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;var query = from f &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; Foo&lt;br /&gt;    select &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; { Foo = f };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var item &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; query)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;    //Do something&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;In this case, the query is actually executed in the foreach loop.  By implementing an extension method, you can easily change this to use a transaction with the &quot;Read Uncommitted&quot; Isolation Level like so:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;var query = from f &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; Foo&lt;br /&gt;    select &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; { Foo = f };&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (var item &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; query.ToListNoLock())&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;    //Do something&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The extension method is implemented like so:&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; IEnumerableExtension&lt;br /&gt;{&lt;br /&gt;   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; TransactionScope CreateNoLockTransaction()&lt;br /&gt;   {&lt;br /&gt;       var options = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; TransactionOptions&lt;br /&gt;       {&lt;br /&gt;           IsolationLevel = IsolationLevel.ReadUncommitted&lt;br /&gt;       };&lt;br /&gt;       &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; TransactionScope(TransactionScopeOption.Required, options);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;static&lt;/span&gt; List&amp;lt;T&amp;gt; ToListNoLock&amp;lt;T&amp;gt;(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; query)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; (TransactionScope ts = CreateNoLockTransaction())&lt;br /&gt;       {&lt;br /&gt;           &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; query.ToList();&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;And then of course you can implement new versions of various other extension methods on IEnumerable/IQueryable, such as SingleOrDefault, FirstOrDefault, etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that this is the same as adding (NOLOCK) hints to EVERY table in your query.  I do not currently know of a way to add nolock hints to individual tables.</content><link rel='replies' type='application/atom+xml' href='http://ryancoder.blogspot.com/feeds/3889711695362823083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ryancoder.blogspot.com/2010/06/execute-linq-queries-without-locking.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/3889711695362823083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3561617962469253143/posts/default/3889711695362823083'/><link rel='alternate' type='text/html' href='http://ryancoder.blogspot.com/2010/06/execute-linq-queries-without-locking.html' title='Execute LINQ queries without locking any tables'/><author><name>Ryan Hennig</name><uri>http://www.blogger.com/profile/00825532811928979346</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>