August 19, 2019 at 7:17 pm
Awesome! Thank you Jonathan I will try that! Do I modify my query to use the newly created IX_DocumentLog_1 or would I keep it the same?
The query will remain the same, the optimiser will work out it needs to use it.
So just run the statement:
CREATE INDEX IX_DocumentLog_1 ON elliedb.DocumentLog(Title, OrderDateUtc)
INCLUDE (Company, RequestedBy, Stage, Status, EncompassId);
In SSMS and SQL Server will do the rest.
August 19, 2019 at 10:32 pm
And here is the DDL (I think)
With one exception, all of the VARCHAR columns in that table are VARCHAR(MAX), which is a LOB datatype. That's the first thing that needs to be fixed.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 19, 2019 at 11:18 pm
Jeff, which ones are VARCHAR(MAX)? Are those the fields with length -1?
August 20, 2019 at 12:11 am
awalton wrote:And here is the DDL (I think)
With one exception, all of the VARCHAR columns in that table are VARCHAR(MAX), which is a LOB datatype. That's the first thing that needs to be fixed.
Yes, I didn't look at the DDL, the OP won't be able to create an index on Title unless it's changed to a smaller varchar.
August 20, 2019 at 12:12 am
Jeff, which ones are VARCHAR(MAX)? Are those the fields with length -1?
Yes. You can verify that by right clicking on the table in the Explorer window of SSMS and generating the script for the table. That's also what people actually want when they ask you to "post the DDL". It should also include all constraints, indexes, and triggers on the table.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 20, 2019 at 12:38 am
Jeff Moden wrote:awalton wrote:And here is the DDL (I think)
With one exception, all of the VARCHAR columns in that table are VARCHAR(MAX), which is a LOB datatype. That's the first thing that needs to be fixed.
Yes, I didn't look at the DDL, the OP won't be able to create an index on Title unless it's changed to a smaller varchar.
The real key here is that all of those MAX columns need to be "right sized". Since 2005, all of that LOB stuff gets stored in-row by default and there's no sense at all in having something a "status" column being assigned as a MAX datatype even if you do drive them out of row. In fact, only the order date column is "right-sized" in this table. While I do believe and agree that "premature optimization is the root of all evil" as Knuth's parable goes, this is just wicked bad design and needs to be fixed.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 20, 2019 at 12:47 am
Upon a quick google I think I should be looking for a Tasks entry on the right click menu, however I don't see that as an option when I right click on the table.
Nope. Your graphic has the right thing on it and whatever your "quick google" stated is incorrect. You need to click on that "Script Table as" selection and then follow your nose.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 20, 2019 at 7:04 pm
The Database I'm working on is a replication of the database within the third party application that my company uses to conduct business. I'm sure they set up the tables on their end to be the VARCHAR(max), it seems that just about every field in this table is assigned a VARCHAR(max) data type (there are about 50 other fields that I didn't show yall). Would I be able to reassign the length on my end and still accept the data that we get from the replication? Like I change the Title column in my database to Varchar(40) and the incoming data is still Varchar(max).
August 20, 2019 at 7:28 pm
You should find out what the max length is of any of the data currently on the database
SELECT MAX(LEN(Id)) MAXId,
MAX(LEN(Company)) MAXCompany,
MAX(LEN(RequestedBy)) MAXRequestedBy,
MAX(LEN(Stage)) MAXStage,
MAX(LEN(Title)) MAXTitle,
MAX(LEN(Status)) MAXStatus
FROM [Staging].[elliedb].[DocumentLog]
Then make your columns a bit longer than the results.
It might also be worth trying to contact the third party vendor to see what they think the maximum lengths should be.
August 20, 2019 at 8:07 pm
Thank you! So there shouldn't be an issue changing the data type of the columns on my end with importing the data of the Varchar(max) type that we receive from that replication?
August 20, 2019 at 9:27 pm
Thank you! So there shouldn't be an issue changing the data type of the columns on my end with importing the data of the Varchar(max) type that we receive from that replication?
I don't know how the data is replicated so can't say. The column you really need to change the data type of is Title. If you can't do that just use the index SQL Server has suggested as it will be able to create that one.
August 21, 2019 at 8:52 pm
Just an update, adding the index that SSMS suggested cut the runtime of the query from ~26 minutes to 3 seconds. Thank you for opening my eyes to the Execution Plan and Indexing Tables. I'll continue to look into assigning the correct length for these columns to further optimize querying this database, but thank yall so much.
Viewing 15 posts - 16 through 29 (of 29 total)
You must be logged in to reply to this topic. Login to reply