How to use sql to ignore a part of the table - sql

I have an SQL table with 500,000 records in orders table.
The sql have been used for past 5 years and every year there are about 100,000 records added on the database.
The table has about 30 fields , one of the fields is "OrderDate"
The query needs only records for the last few months, maximum past 12 months.
so all the records before that are just useless and slow down all the query.
query is slow, and takes 3-4sec, same query was almost immediate few years ago.
i have to load and print all fields columns at once.
Can i make the SQL ignore and not look through part of records, suppose records with OrderDate before 2013, or first 400,000 records or ignore certain part of the records without deleting them?

As far as I can see you have two options:
Creating a new table which is identical to the old one and insert there the rows that you want to ignore, then, delete those same rows from the original table. This solution is good in the case that those rows are "useless" to every query (and if it's viable, you can update other queries that make use of those rows).
Index the column.

This is a classic use of table partitioning, but we don't know what type of SQL you're using so we don't know if it supports it. Add a tag for the version of SQL (SQL Server? Oracle?)

Related

Filter out rows in a SQL Server table based on added-on-date

I have a table in my SQL Server which I update on a monthly basis with new data from the Client. Unfortunately, there is no timestamp which tells me when the rows were added or modified. This becomes a problem when sometimes the same data gets appended twice, and I need to manually check and delete the repeated rows. Is there a way to write a query like below:
DELETE FROM (table)
WHERE (date_time_when_row_is_added) <= (manually_specified_datetime)

Splitting table into two parts: old data and recent data

I have a table records with structure
userId
messageId
message
timestamp
This table grows pretty large so I want to split it into two
records which will have only data for the last 30 days
and records_history which will have all the data, so most of the queries will hit only records table.
What is the best way to achieve this using Oracle? Writing a trigger or something else?

How to repeat records in access table number of times

I need your assistance to figure out how to achieve the following in MS access database.
I have a table with a lot of columns but one of them has a numeric value that will be used as how many times will the record will be repeated.
I need to make another table with repeated records based on Count column.
Build a numbers (aka tally) table (you can google it). I'll call it tblNumbers. Then all you need to do is create a query SELECT <yourTable>.* FROM <yourTable>, tblNumbers WHERE tblNumbers.Number <= <yourTable>.<numberField>

Is there a way to tell the last date time a table was modified in SQL Server 2012

Is there a way to tell the last date time any table records were modified in SQL Server 2012? without having to put in a column for last update in table.
The idea is not to query say a 10 million record table when no rows were changed.
Is there a way to tell the last date time any table records were
modified in SQL Server 2012? without having to put in a column for
last update in table.
Yes - you can write an update/insert/delete trigger on the table that records the fact that an update happened along with the date and time.
The idea is not to query say a 10 million record table when no rows
were changed.
You might also want to look at the built in Change Tracking features

Sql Server 2008 partition table based on insert date

My question is about table partitioning in SQL Server 2008.
I have a program that loads data into a table every 10 mins or so. Approx 40 million rows per day.
The data is bcp'ed into the table and needs to be able to be loaded very quickly.
I would like to partition this table based on the date the data is inserted into the table. Each partition would contain the data loaded in one particular day.
The table should hold the last 50 days of data, so every night I need to drop any partitions older than 50 days.
I would like to have a process that aggregates data loaded into the current partition every hour into some aggregation tables. The summary will only ever run on the latest partition (since all other partitions will already be summarised) so it is important it is partitioned on insert_date.
Generally when querying the data, the insert date is specified (or multiple insert dates). The detailed data is queried by drilling down from the summarised data and as this is summarised based on insert date, the insert date is always specified when querying the detailed data in the partitioned table.
Can I create a default column in the table "Insert_date" that gets a value of Getdate() and then partition on this somehow?
OR
I can create a column in the table "insert_date" and put a hard coded value of today's date.
What would the partition function look like?
Would seperate tables and a partitioned view be better suited?
I have tried both, and even though I think partition tables are cooler. But after trying to teach how to maintain the code afterwards it just wasten't justified. In that scenario we used a hard coded field date field that was in the insert statement.
Now I use different tables ( 31 days / 31 tables ) + aggrigation table and there is an ugly union all query that joins togeather the monthly data.
Advantage. Super timple sql, and simple c# code for bcp and nobody has complained about complexity.
But if you have the infrastructure and a gaggle of .net / sql gurus I would choose the partitioning strategy.