SQL Server Change Tracking data size - sql-server-2008

I have enabled Change Tracking for one of our busy servers. I want to know if there's a way to find out what's the additional space consumed by enabling this feature.
I need this information to set the retention policy. We are debating between 7-14 days. Since this is a busy server, we don't know the impact of enabling this feature on space growth.
Is this information stored in meta-data table? Can I use TSQL to get this information.

A change tracking table is a type of internal table having an object_id. The row count and number of pages allocated for an internal table can be determined from the sys.dm_db_partition_stats in a similar way as user tables. The following query illustrates.
select ##SERVERNAME AS server_name,
sct2.name as schema_name,
sot2.name as table_name,
SUM(ps2.row_count) as table_rows,
(SUM(ps2.reserved_page_count)*8024)/1024/1024 as table_size_mb,
MAX(CAST(ctt.is_track_columns_updated_on AS TINYINT)) AS ct_enabled,
MAX(ps1.row_count) as ct_rows,
(MAX(ps1.reserved_page_count)*8024)/1024/1024 as ct_size_mb,
MAX(ctt.cleanup_version) AS ct_cleanup_ver, /*cleanup may have removed data up to this version */
MAX(ctt.min_valid_version) AS ct_minimum_ver /*syncing applications should only expect data on or after this version */
FROM sys.internal_tables it
JOIN sys.objects sot1 on it.object_id=sot1.object_id
JOIN sys.schemas AS sct1 ON sot1.schema_id=sct1.schema_id
JOIN sys.dm_db_partition_stats ps1 ON it.object_id = ps1. object_id AND ps1.index_id in (0,1)
LEFT JOIN sys.objects sot2 on it.parent_object_id=sot2.object_id
JOIN sys.change_tracking_tables AS ctt ON ctt.object_id = sot2.object_id
LEFT JOIN sys.schemas AS sct2 ON sot2.schema_id=sct2.schema_id
LEFT JOIN sys.dm_db_partition_stats ps2 ON sot2.object_id = ps2. object_id AND ps2.index_id in (0,1)
WHERE it.internal_type IN (209, 210)
GROUP BY sct2.name, sot2.name
ORDER BY sct2.name, sot2.name;

Related

MariaDB : Help a noobie to use timerange

I don't know a lot about MyQSL.
Here is my code (to extract code from a monitoring tool)
SELECT DISTINCT
FROM_UNIXTIME(data_bin.ctime), index_data.host_name, index_data.service_description, metrics.metric_name, data_bin.value
FROM metrics
inner join index_data ON index_data.id = metrics.index_id
inner join data_bin ON data_bin.id_metric = metrics.metric_id
ORDER BY data_bin.ctime;
Despites my efforts, I don't manage to :
Filter by timerange (with human readable time)
Obtain that output :
Timerange, Hostname, ServiceA, metricA1, metricA2, ... ServiceB, metricB1, metricB2 ...
-> the goal is to group by identical Timerange and Hostname
I think your question is missing some key points, and I'll adjust this if you can specify what you're looking for a bit better with some example data from each of these tables.
But in the meantime, I think I have a good understanding, maybe this is what you're looking for:
SELECT DISTINCT
FROM_UNIXTIME(data_bin.ctime) AS timerange, index_data.host_name, index_data.service_description, metrics.metric_name, data_bin.value
FROM metrics
inner join index_data ON index_data.id = metrics.index_id
inner join data_bin ON data_bin.id_metric = metrics.metric_id
GROUP BY index_data.host_name, timerange
ORDER BY timerange;

How do I add a NOT clause to my SQL query

I would like to add a NOT clause to this SQL query:
Select
members.Member_Id,
members.Title,
members.FirstName,
members.LastName,
members.Po_Box,
members.Street,
members.City,
members.Del,
members.Mobile,
members.eMail,
members.WFTD,
ship_info.Renewal_Date
From
members
Left Join
ship_info on (members.Member_Id = ship_info.Member_Id)
Order By
ship_info.Renewal_Date
The NOT clause is this:
Where Member_Id Not Between 2000 And 3000;
I have tried to place this line in different places, but get an error each time, and since Wamp reports errors in french, find it unhelpful.
Yes there are similar questions like this, but they confuse me more, as I don't understand them enough to modify my script accordingly. I don't know much about arrays and complex code.
I export data from the database for mail merge purposes, and Members whose Ids are in the 2000s are deleted Members, whose Id's were moved from their original Id to that of higher numbers, as I don't like deleting people permanently in case they change their mind later down the track.
Thank you.
You could try it like this:
Select members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
From members
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)
WHERE (members.Member_ID < 2000 OR members.member_ID > 3000)
ORDER BY ship_info.Renewal_Date
Since you are using a JOIN and member_id field resides in both tables of the JOIN, you must specify for which table you need the where statement. Try:
SELECT members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
AND 3000
ORDER BY ship_info.Renewal_Date
According to "Database Administration Fundamentals", the following would be the appropriate syntax:
Select members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
From members
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)
WHERE NOT members.Member_Id BETWEEN 2000 AND 3000
ORDER BY ship_info.Renewal_Date ;
In the previously mentioned book, it states,
" JOIN statements can be specified in either the FROM or the WHERE
clause, but it is recommended that you specify them in the FROM clause
".
As for clause precedence, here is the correct order to follow:
SELECT -- "what" we want
FROM -- "where" to look for it
JOIN
WHERE -- condition
GROUP BY
HAVING
ORDER BY -- sorting order
I think below should work, if not then please specify error which you got, you just need to prefix table name members (better if you use this table name as alias)
SELECT members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
AND 3000
ORDER BY ship_info.Renewal_Date

How to increase performance of complicated MySQL select query?

I am working on project management tool and while generating reports through admin end the loading time for the data from the database is very much > 5 minutes. There are few points which I know would help me to increase the performance but right now I need to have the help in SELECT query
SELECT timesheet_client.organisation as client_name, timesheet_project.title as project_name,
timesheet_task.name as task, CONCAT(timesheet_user.first_name, ' ', timesheet_user.last_name) as resource_name,
timesheet_user.bill_factor, timesheet_client.client_type, sum(spent) as spent, sum(delivered_hours) as delivered_hours,
sum(billable_hours) as billable_hours, comments, color, lock_color, updated_by, updated_by_date, timesheet_user_psdb.grp_id,
timesheet_user_psdb.client_id, timesheet_user_psdb.proj_id, timesheet_user_psdb.task_id, timesheet_user_psdb.uid,
timesheet_user_grp.grp_name
FROM timesheet_user_psdb,timesheet_user, timesheet_client,
timesheet_project,timesheet_task,timesheet_user_grp
WHERE timesheet_user.username=timesheet_user_psdb.uid and
timesheet_client.client_id=timesheet_user_psdb.client_id and timesheet_project.proj_id=timesheet_user_psdb.proj_id and
timesheet_task.task_id = timesheet_user_psdb.task_id and timesheet_user_grp.grp_id=timesheet_user_psdb.grp_id and month =3
AND year = 2017 and month!='' and timesheet_user_psdb.client_id=326
GROUP BY timesheet_user_psdb.task_id,timesheet_user_psdb.uid
ORDER BY timesheet_client.client_type desc,timesheet_client.organisation,timesheet_user_psdb.proj_id,
timesheet_user_psdb.task_id,timesheet_user.uid,timesheet_user_psdb.task_id;
I have already used an index on all the primary keys.
EXPLAIN Output:
Help for this would be highly appreciable.
Give this a try:
SELECT
TC.ORGANISATION AS CLIENT_NAME,
TP.TITLE AS PROJECT_NAME,
TT.NAME AS TASK,
CONCAT(TU.FIRST_NAME, ' ', TU.LAST_NAME) AS RESOURCE_NAME,
TU.BILL_FACTOR,
TC.CLIENT_TYPE, SUM(SPENT) AS SPENT, -- You should specify which table this comes from
SUM(DELIVERED_HOURS) AS DELIVERED_HOURS, -- You should specify which table this comes from
SUM(BILLABLE_HOURS) AS BILLABLE_HOURS, -- You should specify which table this comes from
COMMENTS, -- You should specify which table this comes from
COLOR, -- You should specify which table this comes from
LOCK_COLOR, -- You should specify which table this comes from
UPDATED_BY, -- You should specify which table this comes from
UPDATED_BY_DATE, -- You should specify which table this comes from
TUP.GRP_ID,
TUP.CLIENT_ID,
TUP.PROJ_ID,
TUP.TASK_ID,
TUP.UID,
TUG.GRP_NAME
FROM
TIMESHEET_USER AS TU
LEFT OUTER JOIN
TIMESHEET_USER_PSDB AS TUP
ON TU.USERNAME = TUP.UID
LEFT OUTER JOIN
TIMESHEET_USER_GRP AS TUG
ON TUP.GRP_ID = TUG.GRP_ID
LEFT OUTER JOIN
TIMESHEET_CLIENT AS TC
ON TUP.CLIENT_ID = TC.CLIENT_ID
LEFT OUTER JOIN
TIMESHEET_PROJECT AS TP
ON TUP.PROJ_ID = TP.PROJ_ID
LEFT OUTER JOIN
TIMESHEET_TASK TT
ON TUP.TASK_ID = TT.TASK_ID
WHERE
MONTH = 3 AND -- You should specify which table this comes from
YEAR = 2017 AND -- You should specify which table this comes from
MONTH != '' AND -- You should specify which table this comes from
TUP.CLIENT_ID = 326
GROUP BY
TUP.TASK_ID,
TUP.UID
ORDER BY
TC.CLIENT_TYPE DESC,
TC.ORGANISATION,
TUP.PROJ_ID,
TUP.TASK_ID,
TU.UID,
TUP.TASK_ID;
Doing the EXPLAIN should shed some light on it.
You might see a performance gain by doing the table joins explicitly in the FROM clause instead of inside the WHERE (https://dev.mysql.com/doc/refman/5.7/en/join.html). By doing explicit joins (e.g. LEFT OUTER, etc...) you will not only improve the readability of the query, but may be able to use less expensive joins where needed. This also affects how the query is executed as each clause is executed in a specific order (MySQL query / clause execution order).

MySQL can only use 61 tables in a join; How can I bypass this error for the following query?

I get a MySQL Error saying, I cannot use more than 61 tables in a join. I need to avoid this error. How do I do it? Please Help.
select
view_pdg_institutes.user_id as User_ID,
view_pdg_institutes.institute_id as Teacher_ID,
view_pdg_institutes.institute_name as Institute_Name,
view_pdg_institutes.user_email as Email,
view_pdg_institutes.contact_person_name as Contact_Person,
view_pdg_institutes.alternative_contact_no as Alternative_Mobile_No,
view_pdg_institutes.primary_contact_no as Mobile_No,
view_pdg_institutes.correspondance_address as Address,
view_pdg_institutes.other_communication_mode as Preferred_Contact_Mode,
view_pdg_institutes.size_of_faculty as Size_of_Faculty,
view_pdg_institutes.operation_hours_from as Operation_Hours_From,
view_pdg_institutes.operation_hours_to as Operation_Hours_To,
view_pdg_institutes.teaching_xp as Teaching_Experience,
view_pdg_institutes.installment_allowed as Installment_Allowed,
view_pdg_institutes.about_fees_structure as About_Fees_Structure,
view_pdg_institutes.no_of_demo_class as No_of_Demo_Classes,
view_pdg_institutes.demo_allowed as Demo_Allowed,
view_pdg_institutes.price_per_demo_class as Price_Per_Demo_Class,
view_pdg_tuition_batch.tuition_batch_id as Batch_ID,
view_pdg_batch_subject.subject_name as Subject_Name,
view_pdg_batch_subject.subject_type as Subject_Type,
view_pdg_batch_subject.academic_board as Academic_Board,
view_pdg_batch_fees.fees_type as Fees_Type,
view_pdg_batch_fees.fees_amount as Fees_Amount,
view_pdg_tuition_batch.course_days as Course_Days,
view_pdg_tuition_batch.days_per_week as Days_Per_Week,
view_pdg_tuition_batch.class_duration as Class_Duration,
view_pdg_tuition_batch.class_type as Class_Type,
view_pdg_tuition_batch.course_length as Course_Length,
view_pdg_tuition_batch.course_length_type as Course_Length_Type,
view_pdg_tuition_batch.no_of_locations as No_of_Locations,
view_pdg_tuition_batch.class_capacity_id as Class_Capacity_ID,
view_pdg_tutor_location.locality as Locality,
view_pdg_tutor_location.address as Address,
view_pdg_batch_class_timing.class_timing as Class_Timing
from view_pdg_tuition_batch
left join view_pdg_institutes on (view_pdg_tuition_batch.tutor_institute_user_id = view_pdg_institutes.user_id)
left join view_pdg_batch_subject on (view_pdg_batch_subject.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_fees on (view_pdg_batch_fees.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_class_timing on (view_pdg_batch_class_timing.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_tutor_location on (view_pdg_tutor_location.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
group by view_pdg_tuition_batch.tuition_batch_id;
I need a solution that would not require changing the current approach of writing the query.
I don't think it's possible to do what you're asking without some elaborate changes in the way you store and query data. You can
denormalize your DB to store JSON data;
create materialized views, emulating them via triggers, because they're absent in MySQL;
use temporary tables;
join partial selects by hand at the call site;
compile MySQL with another join limit;
use proper SQL engine like Postgres, that doesn't suffer from such stupid things.
Insert the contents of each view into its own temporary table. Then do the same query with the temporary table names substituted for the original view names.

Using results from a query within the same query

I have a rather big SQL statement that I am working with in MS Access 2010. Here it goes:
SELECT
W.ID AS wid,
W.wpt_ty AS ty,
W.wpt_num AS num,
W.wpt_nxt AS nxt,
W.latdeg AS lat,
W.londeg AS lon,
W.alt AS alt,
W.mission_id AS mid,
W.ctg1 AS ctg1,
W.ctg2 AS ctg2,
W.ctg3 AS ctg3,
W.ctg4 AS ctg4,
W.wpt_index AS indx,
W.vel AS vel,
W.tu AS tu,
R.route_num AS rnum,
R.AC_num AS ac,
R.route_type AS rtype,
R.LastUpdatedOn AS d8,
R.LastUpdatedBy AS auth,
R.flight_wpt_count AS wfcount,
M.mission_name AS msnName,
V.Description AS vstatus,
R.disallowed_reason_id AS did,
CW.wpt_num AS c1num,
CR.matching_route_id AS c1mrid,
CW.wpt_index AS c1indx,
CRU.runway_name AS c1rnwy,
CR.route_num AS c1rnum
FROM Validation AS V
(RIGHT JOIN Runways AS CRU
INNER JOIN (Routes CR
INNER JOIN Waypoints CW ON CR.ID = CW.route_id)
ON Runways.ID = Routes.runway_id
INNER JOIN ((Missions as M
INNER JOIN Routes AS R ON M.ID = R.mission_id)
INNER JOIN Waypoints AS W ON (R.ID = W.route_id)
AND (M.ID = W.mission_id)) ON
V.ID = R.validated
WHERE (((R.matching_route_id)=307543) AND ((R.validated) <> 0 ))
AND (((CW.mission_id)=mid) AND ((CW.wpt_num) = (ctg1))))
If you look at the bottom, you can see am I referencing the values ctg1 and mid on a Right Join while the Inner Joins reference other literal values. Eventually I will want to do the same for ctg2, ctg3, and ctg4
Right now I am running these as 2 separate queries but finding it to be way too slow. If I can join combine the queries (sort of like how I am showing here) it could speed things up greatly. But I am at a loss for how to:
Using select values earlier in said query from the Inner/Left join and push them into values needed on the Right join.
I may be using joins incorrectly, but I thought they had to do with combining data from possible the same tables, just on different pivot points.
How to use the MS Access GUI to help write a query like this.
I know this is for MS Access but I am tagging for MySQL just in case there are similar queries there which can be ported to MS Access?
Have you tried using UNION for this?
It would allow you to execute this query (As two queries, which you mentioned as a possibility), and join the results for your output.
Be warned, it will eat up (only show one of) your duplicates in the results set.
I also suggest reading up on the different types of joins for your own benefit, in the following answer:
MYSQL Joins