Jump to content

Which 2 table structure is better for photo order storing


Recommended Posts

When users can drag and drop photos on a webpage to sort their order in photo album, which version to use for storing the photo orders:

 

Version 1:

Table "Album Categories"

AlbumId, AlbumName, UserID

(3 / Family Album / 32)

 

Table "Photos"

PhotoId, UserId, AlbumId, PhotoOrder

39 / 32 / 3 / 1

56 / 32 / 3 / 4

94 / 32 / 3 / 2

92 / 32 / 3 / 3

78 / 32 / 3 / 5

 

 

 

Version 2:

Table "Album Categories"

AlbumId, AlbumName, UserID, PhotoOrder

(3 / Family Album / 32 / 39,94,92,56,78 )

 

Table "Photos"

PhotoId, UserId, AlbumId

39 / 32 / 3

56 / 32 / 3

94 / 32 / 3

92 / 32 / 3

78 / 32 / 3

UPDATE Photos SET PhotoOrder=PhotoOrder+1 WHERE UserId = 32 AND AlbumId = 3;
UPDATE Photos SET PhotoOrder=1 WHERE UserId = 32 AND AlbumId = 3 AND PhotoOrder = 100;

 

You're right, though.  Version #1 may mean more database operations.  It does, however, provide an easy way to get the pictures out in order (by using the PhotoOrder as the ORDER BY column when retreiving photos).  You wouldn't get that functionality with Version #2.

 

With Version #2 you can use explode() to read that delimited string into an array and write your own re-ordering functions.  And, since you're probably not pulling the pictures out in order, but singly (img src="display.php?id=12345"), you might not need MySQL's ORDER BY.

 

Yeah, actually Version #2 might not be a bad itea.  This is one of the few cases where I can see some advantages to using the delimited string, and they're mostly related to performance.

I'm also thinking that with version 2 when I would select the comma delimited string and then go through the comma separated array as I would go through various rows in version 1, then e.g. if I would display 20 photos per album page, I would have to do only 1 mysql row selection instead of 20 rows in version 1, and just limit the comma separated list to 20 first items.

 

So in theory 1 write instead of 99 writes, 1 read instead of 20 reads.

 

Am I correct in my theory? Or is there a downside I'm not seeing.

Using version #2 and the following query to order the records:

 

SELECT FIND_IN_SET(PhotoId, PhotoOrder) AS orderby, Photos.*
FROM Photos
     INNER JOIN `Album Categories` ON Photos,AlbumId = `Album Categories`.AlbumId
ORDER BY orderby;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.