720 likes | 1.22k Views
SQL Tips and Best Practices for Meditech’s DR. 2011 MUSE International Tuesday Education Session May 31 st. Presenter: Jamie Gerardo. Today’s Agenda. Operational – Key Topics Background Jobs DR Errors Auditing/Validation Development – Key Topics Report Development Standards
E N D
SQL Tips and Best Practices for Meditech’s DR 2011 MUSE International Tuesday Education Session May 31st Presenter: Jamie Gerardo
Today’s Agenda • Operational – Key Topics • Background Jobs • DR Errors • Auditing/Validation • Development – Key Topics • Report Development Standards • Finding Data • Writing Efficient Code • T-SQL Tips
Operational • Background jobs • DR Errors • Audits / Validation • Server Maintenance • Monitor space • Backing up and testing backup restore • Preventative server maintenance • Meditech Website • Logging Tasks • Following recommendations and guidelines
MT – DR Background JobsClient Server http://www.meditech.com/ * Graph from Meditech website
MT – DR Background JobsMagic * Graph from Meditech website
MT – DR Background Jobs 6.0 * Graph from Meditech website
Monitoring/Maintaining Background Jobs • You can create additional bkg jobs (depending on the ring release) • Do not reboot server without stopping jobs (completely) • Background job can be running although a table is not updating • Review Meditech Recommendations and website
Additional Background Jobs Why create additional background jobs? (Current job is taking too long to update the DR tables.) • You can split up larger applications into multiple jobs. • You can segment off particular tables that are used for real time reporting. • You can split off applications that take longer. * Additional jobs depends on the ring release version
Rebooting the DR Server • Do not reboot the server without stopping the background jobs! • What happens if you do? • Table sequences will get off track • Data will be skipped • Table IL’s may need to be done to get data over to the DR. You can stop the background jobs by: • Calling Meditech or • Stopping the jobs through the DR application
DR Errors • DR Parameters • Activity Index • Error Log • Monitor Error Summary • It is not recommended to report every error • Provide as much information as possible • Prioritize DR Errors Logged • See Meditech Website
DR Parameters Activity Days (Typically defaults to 7-10 days): Determines how long activity is kept around in an index to be transferred to DR. Can be set at the table level by Meditech for research/problem issues Error Log Days (Default is 3 days): This should be at least 7-10 days. C/S 6.5 but all Meditech Versions have this option in the DR Parameters (it just looks different)
DR Errors • Only report DR Errors that reference tables you use or will use in the future. • Only report the latest version of the same or similar error. • Report Skipped activity as soon as possible, this data will eventually be removed from the index. Key items when reviewing errors Example not to report: DATE: 20110414 TIME: 0001 TABLE: LACSAX1 LabSpecAlerts Sequence: SOURCEID: OSC MIS DB: OSC PROGRAM: DrXferBkg[K] APPL DB: SCH.SSM DPM: LAB.C.SPEC TYPE: PM Program missing ELEMENT: EXPECTED: ERROR VALUE: ROW: MESSAGE: SCH.SSM's LabSpecAlerts expander is missing Example to report: DATE: 20110221 TIME: 1143 TABLE: VISITCLI AdmVisitClinicalQueries Sequence: 4391 SOURCEID: OSC MIS DB: OSC PROGRAM: DrXferBkg[T:ADMQUERY] APPL DB: ADM.OSC DPM: ADM.PAT TYPE: S Socket ELEMENT: SQL EXPECTED: ERROR VALUE: ROW: 873550 MIO5UD Y 1 MESSAGE: Primary key VisitD is missing This is typically not a table that will ever be used so don’t report the error
DR Error Messages Always confirm with your applications specialist. These are some general guidelines. • Typical Messages you can ignore: MESSAGE: Unable to open prefixes to ADM.OSH database [1] MESSAGE: Missing subscript at position 0 for OE.STAT.transcription.stats MESSAGE: Violation of PRIMARY KEY constraint 'mtpk_ep551386'. Cannot insert duplicate key in object 'DMisUserC~. Check Server Error Log for more info SQL Non Fatal Error MESSAGE: Line 1: Incorrect syntax near '{'.. Check Server Error Log for more info SQL NonFatal Error MESSAGE: Unable to find segment from physical [No prefix for segment [Physical.] and nil in @.db] MESSAGE: No pointer for include children of AP.AHS.TmpOpIC table MESSAGE: Syntax error converting datetime from character string.. Check Server Error Log for more info SQL Non Fatal Error <EP550409> MESSAGE: CON DR Server not responding to ECB command - ACK 98 SQL Fatal Error <VISITCLI> DR Server not responding to ECB command - ACK 98 • Messages you want to report: MESSAGE: AdmVitalSigns Activity skipped, not in Pending status MESSAGE: Primary key CheckID is missing MESSAGE: Missing subscript at position 1 for MRI.DRC.insure.order MESSAGE: Unknown error [SYS] - Segment A is down, unable to start DrXferBkg[T:ADMQUERY]
Logging DR Errors Include ring release version in description along with table name and indication of the issue. Example Descriptions: • 5.61 AdmVisitQueries Primary Key Missing VisitID • 5.65 MRI.DRC.insure.order - Missing subscript Pos 1 • 5.61.1 OeOrderQuery - Expander is missing Issue: DR #6133704 (Mar 2, 2009) Status: Open Priority: Routine Priority Lists: #3 on DR Priority List Description: 5.61.13 LabSpecimenTests - Column Discrepancies Request Type: Problem Customer Contact: James Durbin (617-555-1212) Issue Notification: james@whatsuphosp.org (All Edits) Module Notifications: ACME,SMH (jmcdonald@acmeware.com) - MEDITECH Edits Set a Priority List on your tasks to get the quickest resolution
Auditing / Validation • Blue Elm DR Auditor or other tool? • Audit tables every 1-2 months • Re-Analyze (and within 2 days) • Research audits and provide MT recent examples (within the activity Index) • Application purge parameters • Manual validation • Matching NPR / DR Reports • Meditech Auditing Tool • CS 5.65, Magic 5.64.17(?), 6.0 • Is in its infancy with development and testing.
Blue Elm DR Auditor • More DR Auditor Tips • Start with auditing tables you use • Audit like tables • Only a few tables at a time • Rebuild Schemas after a new Meditech ring release • Recreate audits after an update • Delete old audits
DR Auditor – A step further You can query the audits to find specific examples or the most recent examples. You can also confirm that the data is still missing. Meditech tables and corresponding DRAuditor table names:
Reporting discrepancies to Meditech • Meditech would prefer audits that are not older than a week. Once the data has been purged from the Activity index it makes it difficult to troubleshoot this issue. • Typically log 1 table per Task • Task descriptions should include the Table Name along with “Missing Row” or “Column Discrepancy” • Ex: 5.65 AdmVisits – Missing rows • Attach the HTML Report in your task or provider recent examples from your sql query.
DR Maintenance • Overall Server Maintenance • Monitor disk space • Database Backups • Database Maintenance • DBCC’s on a monthly basis • Integrity checks (using wizard) • Analyze and repair index fragmentation on a monthly basis
Details of Maintenance • Monitor disk space • Database space • How much space is the database using • Drive space • Always have 25% of total disk space as free (livedb – E: drive) • Index defragmenting • Don’t use the database maintenance wizard • Wizard will drop and recreate indexes, which you don’t want to do.
Defragging Indexes Logical Scan Fragmentation – lower is better, anything above 30-35% would be a candidate to defragment. Avg. Page Density – higher is better, ideally 90-95%. The fuller each page is, the denser (less fragmented) the data are. DETERMINING FRAGMENTATION AT THE TABLE AND INDEX LEVEL DEFRAG INDEX IF NECESSARY DBCC INDEXDEFRAG ( DBNAME, TABLENAME, INDEXNAME)
SQL Development • Finding Data • How can you find data in the DR? • Organization • Database, Stored Procedures and Tables • Efficiency • How to make your code run fast and more efficiently • T-SQL TIPS • Helpful code tips
Finding Data • If you know the NPR structure then finding data will be much easier • In general you can think of a Detail Segment as a table Tools • Meditech Website • SysDrTables • Shift F9 (C/S) and Shift F8 for Magic • DR Application Menu
Meditech DR Home Page Overview of the data repository * Table Structure Customer Responsibilities
Viewing the Table Structurehttp://www.meditech.com/ Useful Table information
Meditech - Table Structure • Shows the equivalent NPR – Parent/Child relationships • Interactive Primary Keys that displays other tables with foreign keys
SysDr Tables in livedb --------------------------------------------------------------------------- -- A general search by DR Field Name -- '%Comment%' is a wildcard search for any field with Comment -- You can modify the name as needed for your search --------------------------------------------------------------------------- SELECT T.Name, C.* FROM livedb.dbo.SysDrColumns C INNER JOIN livedb.dbo.SysDrTables T ON C.TableID = T.TableID WHERE C.Name like '%Comment%' order by 1 Examples of how to query Sys DR tables ------------------------------------------- -- A search by specific NPR field ------------------------------------------- SELECT T.Name, C.* FROM livedb.dbo.SysDrColumns C INNER JOIN livedb.dbo.SysDrTables T ON C.TableID = T.TableID WHERE C.NprElement = 'BAR.PAT.account' order by 1 ------------------------------------------------------- -- A search by Dpm and Detail Segment ------------------------------------------------------- SELECT T.Name, C.* FROM livedb.dbo.SysDrColumns C INNER JOIN livedb.dbo.SysDrTables T ON C.TableID = T.TableID WHERE C.NprDpm = 'BAR.PAT' AND C.NprSegment = 'main' order by 1
SysDr Result Example SELECT T.Name, C.* FROM livedb.dbo.SysDrColumns C INNER JOIN livedb.dbo.SysDrTables T ON C.TableID = T.TableID WHERE C.Name like '%Comment%' order by 1 Shows table name, column, data type along with the DPM, NprSegment and NprElement
Table and Field Inquiry on the DR Application Menu I rarely use these tools on the DR Menu – They can be helpful but I find the previously covered options to be the best. Magic 5.61 Screen
Organization - Database • Don’t save stored procedures and/or tables in livedb. • Create a database to keep your stored procedures, views and tables. • Make sure Recovery Mode is set to simple • You can set up the database files similar to livedb • You will want to include the database you create in your backup plan.
What is a stored procedure? A stored procedure is a saved set of code on the sql server that allows you to run: EXEC spBarAccountsByAccountType Rather than…… SELECT BVFD.AccountType, BV.PostalCode, BV.AccountNumber FROM livedb.dbo.BarVisits BV INNER JOIN livedb.dbo.BarVisitFinancialData BVFD ON BV.SourceID = BVFD.SourceID AND BV.BillingID = BVFD.BillingID
Organize your Stored Procedures • Name your stored procedures so that you can easily locate them. • Ex: spErDepartVolumesByHour • Ex: spAdmRevisitsTable • Re-name stored procs no longer in use. • Ex: x_spErDepartVolumesByHour • Use Header information to describe a stored procedure’s use. • Only save useable code as a stored procedure. • Save test code and research code as a text file or label appropriately.
Stored Procedures Once created – you can Modify and View Dependencies To save a stored procedure you CREATE PROC. This saves the stored procedure on the server/database you’ve selected
Saving code as a text file • Items to save as a text file • Research queries • Testing queries Default Location – this can be changed
Creating Efficiencies • Indexing • Estimated Execution Plan • Joining on Primary Keys • Creating Datamarts (tables) • Using temp tables vs. sub queries • Functions • Using With (NoLock)
Table Indexing • Clustered • Every Meditech table has a clustered index, which is the physical order of the table by primary key(s). Never modify or delete • There is only 1 per table • Non-Clustered • A non-clustered index creates a separate 'internal' table that stores only the selected key values of the table in order. Each 'record' in this index contains the key value from one record in the table along with a pointer to either the data record itself or to a value in the clustered index. • A max of 249 non-clustered indexes per table – You will want to be selective with indexes you build
Typical Indexes used • BarCollectionTransactions • ixReceiptDateTime • ixInsuranceID • AdmittingData • ixAdmitDateTime • AbstractData • ixVisitID • DMisUserStatisticsDetail • ixAccountNumber (Field4) • ixUnitNumber (Field3) • BarChargeTransactions • ixServiceDateTime • ixTransactionProcedureID • ixProcedureChargeDept • BarVisits • ixVisitID • ixAdmitDateTime • ixServiceDateTime • AdmVisits • ixServiceDateTime • ixStatus
Example of Execution Plan Display Estimated Execution Plan
Joining on Primary Keys • Joining on the primary keys will make your report run faster. • Omitting the primary key will not only slow down your query but many times your output will not be correct. • Each application has a unique identifier (primary key) that will allow you to join to other applications.
Primary Key Example Even if there is only one SourceID, you will want to use the Clustered Index for faster processing. SELECT AV.VisitID, AV.LocationID, AV.[Name], AD.AdmitDateTime FROM livedb.dbo.AdmVisits AV WITH (NOLOCK) INNER JOIN livedb.dbo.AdmittingData AD WITH (NOLOCK) ON AV.SourceID = AD.SourceID AND AV.VisitID = AD.VisitID WHERE Status='ADM IN‘
SQL Design Query Editor You can use this tool but you still need to join on the primary keys. This tool will not automatically do that for you.
Primary Key Example 2 VisitID is in a number of tables but you’ll only want to use it to join to a parent type table - BarVisits, AbstractData, Lab Specimens, SchAppointments SELECT AV.VisitID, AV.LocationID, AV.[Name], AD.AdmitDateTime, BV.PrimaryInsuranceID, BVFD.Balance FROM livedb.dbo.AdmVisits AV INNER JOIN livedb.dbo.AdmittingData AD ON AV.SourceID = AD.SourceID AND AV.VisitID = AD.VisitID LEFT JOIN livedb.dbo.BarVisits BV ON AV.SourceID = BV.SourceID AND AV.VisitID = BV.VisitID LEFT JOIN livedb.dbo.BarVisitFinancialData BVFD ON BV.SourceID = BVFD.SourceID AND BV.BillingID = BVFD.BillingID where Status='ADM IN' • Use the application’s primary key (unique identifier) within the application tables. • Adm - VisitID • Bar – BillingID • Abs – AbstractID • Oe – OrderID • Sch - AppointmentID
Creating Datamarts / Tables When do you create a new table? • Processing time for a report may dictate the need for a table. • Combining data from various applications for faster queries. • Combining data and creating custom fields for reporting needs. • Building Report Models
Datamart Example Issue: You have a very large query for nursing with lots of data from various assessments and with pharmacy data. This query takes over 10 minutes to run and will take longer if additional filters are added. Solution: • Create a table with all the data you need for your reports • Set up a SQL Server Agent Job to populate the data on a nightly basis • Create a set of store procedures to use for your report output.
Datamart Example - Continued NursingStoredProcedure This table gets updated daily. A SQL Server Job is set up to populate on a nightly basis with new data. Additional Stored procedures are written to use for reporting purposes NursingLocationSummary
Creating Tables After the first table insert typically you will: TRUNCATE TABLE PatientBirthDates INSERT PatientBirthDates • You can write sql code to insert data into a table by first creating the table then inserting the data. 1. CREATE TABLE PatientBirthDates ( Name varchar(35) not null, AccountNumber varchar(15) not null, DOB datetime null) 2. INSERT PatientBirthDates SELECT Name, AccountNumber, BirthDateTime FROM AdmVisits • You can insert directly into a table as below. The data types and fields will populate based on the data returned. Select * INTO tbNewTable from DMisUsers Either way of creating the table is fine. Just note that you may need alter data types and write code to add Primary keys. Typically modifications will need to be made when the table is created by an ‘INTO’. You’ll also need to Truncate Table where necessary. ** Will demonstrate The INTO only works the first time.
User Defined Function What is a User Defined Function? Functions are subroutines used to encapsulate frequently performed logic. Any code that must perform the logic incorporated in a function can call the function rather than having to repeat all of the function logic. • Built-in functions operate as defined in the Transact-SQL Reference and cannot be modified. The functions can be referenced only in Transact-SQL statements using the syntax defined in the Transact-SQL Reference. • Examples AVG, SUM, COUNT,DATEADD, DATEDIFF,NAME, ETC.. • User-defined functions allow you to define your own Transact-SQL functions using the CREATE FUNCTION statement. For more information about these built-in functions • This is what we’ll be doing in class.