European Windows 2012 Hosting BLOG

BLOG about Windows 2012 Hosting and SQL 2012 Hosting - Dedicated to European Windows Hosting Customer

SQL Server 2014 Hosting Russia - HostForLIFE.eu :: Restart Interrupted Restore Operations

clock January 8, 2015 07:19 by author Peter

Circumstances in which a restore operation is hindered are not exceptionally remarkable. This is the reason, in this post, we will demonstrate to you what you need to do with a specific end goal to restart the interrupted operation using T-SQL queries in SQL Server 2014.

In the event that your restore operation was interrupted, you can at present restart the methodology and proceed starting there where it got intruded.

This is a peculiarity that can be extremely valuable in the event that you have huge databases which you need to restore. On the off chance that the methodology of restoring falls flat near to the end, the majority of the times you can restart the whole operation from the point where it cleared out off, as opposed to restarting the whole restore procedure of the database.

To be particular, when you make your restore from tape, you can restart from the current tape as opposed to restarting from the first. Anyhow, if the restore was in the stage when it was being rolled forward, then no information will be replicated from that backup set.

Restart interrupted restore with T-SQL
In the event that you have utilized a T-SQL query to restore your database and the methodology was hindered for any reason, known or obscure, then what you need to do is to define a RESTART proviso toward the end of the same inquiry and run the question yet again.

Let’s assume that you have the following query, or something similar, and it got interrupted during execution.
-- Restore a full database backup of myDB database
RESTORE DATABASE myDB
FROM DISK = 'C:\myDB.bak'
GO

Presently, keeping in mind the end goal to proceed with and interrupted restore operations, connected for our situation, we are going to utilize the query from above completed with the WITH RESTART clause.

-- Just run the initial RESTORE statement specifying WITH RESTART in order to restart interrupted restore operations
RESTORE DATABASE myDB
FROM DISK = 'C:\myDB.bck'
WITH RESTART
GO



European SQL 2014 Hosting - UK :: How to Join The First Row in SQL

clock August 14, 2014 12:41 by author Onit

To easily making report, we need a list of users and the most recent widget each user has created. We have a users table and a widgets table, and each user has many widgets. users.id is the primary key on users, and widgets.user_id is the corresponding foreign key in widgets.

To solve this problem, we need to join only the first row. There are several ways to do this. Here are a few different techniques and when to use them.

Use Correlated Subqueries when the foreign key is indexed

Correlated subqueries are subqueries that depend on the outer query. It's like a for loop in SQL. The subquery will run once for each row in the outer query:

select * from users join widgets on widgets.id = (
    select id from widgets
    where widgets.user_id = users.id
    order by created_at desc
    limit 1
)

Notice the where widgets.user_id = users.id clause in the subquery. It queries the widgets table once for each user row and selects that user's most recent widget row. It's very efficient if user_id is indexed and there are few users.

Use a Complete Subquery when you don't have indexes

Correlated subqueries break down when the foreign key isn't indexed, because each subquery will require a full table scan.

In that case, we can speed things up by rewriting the query to use a single subquery, only scanning the widgets table once:

select * from users join (
    select distinct on (user_id) * from widgets
    order by user_id, created_at desc
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id

This new subquery returns a list of the most recent widgets, one for each user. We then join it to the users table to get our list.

We've used Postgres' DISTINCT ON syntax to easily query for only one widget peruser_id.

Use Nested Subqueries if you have an ordered ID column

In our example, the most recent row always has the highest id value. This means that even without DISTINCT ON, we can cheat with our nested subqueries like this:

select * from users join (
    select * from widgets
    where id in (
        select max(id) from widgets group by user_id
    )
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id

We start by selecting the list of IDs repreenting the most recent widget per user. Then we filter the main widgets table to those IDs. This gets us the same result as DISTINCT ON since sorting by id and created_at happen to be equivalent.

Use Window Functions if you need more control

If your table doesn't have an id column, or you can't depend on its min or max to be the most recent row, use row_number with a window function. It's a little more complicated, but a lot more flexible:

select * from users join (
    select * from (
        select *, row_number() over (
            partition by user_id
            order by created_at desc
        ) as row_num
        from widgets
    ) as ordered_widgets
    where ordered_widgets.row_num = 1
) as most_recent_user_widget
on users.id = most_recent_user_widget.user_id
order by users.id

The interesting part is here:

select *, row_number() over (
    partition by user_id
    order by created_at desc
) as row_num
from widgets
over (partition by user_id order by created_at desc

specifies a sub-table, called a window, per user_id, and sorts those windows by created_at desc.row_number() returns a row's position within its window. Thus the first widget for each user_id will have row_number 1. 

In the outer subquery, we select only the rows with a row_number of 1. With a similar query, you could get the 2nd or 3rd or 10th rows instead.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in